Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
>
说一下具体方式,首先java *** 作office需要有第三方的jar来支持比如poi-3
2
jar、jacob
jar、jxl
jar、poi-contrib-3
2-FINAL-20081019
jar、poi-scratchpad-3
2-FINAL-20081019
jar之类的jar,有了这些第三方包的支持,然后根据自己的需求来对照包里面具体的工具类来实现,自动播放和自动下来观看建议自己写个timer来实现java
util
Timer和java
util
TimerTask
controller
@RequestMapping("assign2/downloadAssignWayConfigHistorydo")public void downloadAssignWayConfigHistory(>}
service
public void exportAssignCaseWayHistory(String[] exceltitles,ArrayList<Map<String, Object>> excelDatas, OutputStream out) {
try{
// 第一步,创建一个workbook,对应一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet hssfSheet = workbookcreateSheet("sheet1");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = hssfSheetcreateRow(0);
//不带背景颜色居中的cellStyle
HSSFCellStyle hssfCellStyle = workbookcreateCellStyle();
//居中
hssfCellStylesetAlignment(HSSFCellStyleALIGN_CENTER);
hssfCellStylesetVerticalAlignment(hssfCellStyleVERTICAL_CENTER);
//带背景和字体颜色并且居中的cellStyle
HSSFCellStyle hssfCellStyleWithBackgroundColor = workbookcreateCellStyle();
//居中
hssfCellStyleWithBackgroundColorsetAlignment(HSSFCellStyleALIGN_CENTER);
hssfCellStyleWithBackgroundColorsetVerticalAlignment(hssfCellStyleVERTICAL_CENTER);
//背景
hssfCellStyleWithBackgroundColorsetFillForegroundColor(IndexedColorsYELLOWgetIndex());
hssfCellStyleWithBackgroundColorsetFillPattern(hssfCellStyleSOLID_FOREGROUND);
//字体
HSSFFont font = workbookcreateFont();
fontsetColor(HSSFColorREDindex);
hssfCellStyleWithBackgroundColorsetFont(font);
HSSFCell hssfCell = null;
for (int i = 0; i < exceltitleslength; i++) {
hssfCell = rowcreateCell(i);//列索引从0开始
hssfCellsetCellValue(exceltitles[i]);//列名1
hssfCellsetCellStyle(hssfCellStyle);//列居中显示
//设置列宽
if (i == 1 || i == 6 || i == 7) {
hssfSheetsetColumnWidth(i, 5000);
}else {
hssfSheetsetColumnWidth(i, 3000);
}
}
// 第五步,写入实体数据
for (int i = 0; i < excelDatassize(); i++) {
row = hssfSheetcreateRow(2i+1); //奇数行
Map<String, Object> excelData = excelDatasget(i);
// 第六步,创建单元格,并设置值
String updateBy = (String) excelDataget("updateBy");
String updateDate = (String) excelDataget("updateDate");
String region = (String) excelDataget("region");
String branch = (String) excelDataget("branch");
String center = (String) excelDataget("center");
String dept = (String) excelDataget("dept");
String assignMode = (String) excelDataget("assignMode");
String newAssignMode = (String) excelDataget("newAssignMode");
String turnableDimension = (String) excelDataget("turnableDimension");
String newTurnableDimension = (String) excelDataget("newTurnableDimension");
String M0 = (String) excelDataget("M0");
String M1 = (String) excelDataget("M1");
String M2 = (String) excelDataget("M2");
String M3 = (String) excelDataget("M3");
String newM0 = (String) excelDataget("newM0");
String newM1 = (String) excelDataget("newM1");
String newM2 = (String) excelDataget("newM2");
String newM3 = (String) excelDataget("newM3");
HSSFCell cell0 = rowcreateCell(0);
cell0setCellStyle(hssfCellStyle);
cell0setCellValue(updateBy);
HSSFCell cell1 = rowcreateCell(1);
cell1setCellStyle(hssfCellStyle);
cell1setCellValue(updateDate);
HSSFCell cell2 = rowcreateCell(2);
cell2setCellStyle(hssfCellStyle);
cell2setCellValue(region);
HSSFCell cell3 = rowcreateCell(3);
cell3setCellStyle(hssfCellStyle);
cell3setCellValue(branch);
HSSFCell cell4 = rowcreateCell(4);
cell4setCellStyle(hssfCellStyle);
cell4setCellValue(center);
HSSFCell cell5 = rowcreateCell(5);
cell5setCellStyle(hssfCellStyle);
cell5setCellValue(dept);
HSSFCell cell6 = rowcreateCell(6);
cell6setCellStyle(hssfCellStyle);
cell6setCellValue(assignModeequals("0")"模式1":"模式2");
if ("BRANCH"equals(turnableDimension)) {
turnableDimension = "分部";
} else if ("CENTER"equals(turnableDimension)) {
turnableDimension = "中心";
} else if ("DEPT"equals(turnableDimension)) {
turnableDimension = "部";
}
HSSFCell cell7 = rowcreateCell(7);
cell7setCellStyle(hssfCellStyle);
cell7setCellValue(turnableDimension);
HSSFCell cell8 = rowcreateCell(8);
cell8setCellStyle(hssfCellStyle);
cell8setCellValue(M0equalsIgnoreCase("Y")"案2":"案2");
HSSFCell cell9 = rowcreateCell(9);
cell9setCellStyle(hssfCellStyle);
cell9setCellValue(M1equalsIgnoreCase("Y")"案1":"案2");
HSSFCell cell10 = rowcreateCell(10);
cell10setCellStyle(hssfCellStyle);
cell10setCellValue(M2equalsIgnoreCase("Y")"案1":"案2");
HSSFCell cell11 = rowcreateCell(11);
cell11setCellStyle(hssfCellStyle);
cell11setCellValue(M3equalsIgnoreCase("Y")"案1":"案2");
row = hssfSheetcreateRow(2i+2); //偶数行
HSSFCell newCell6 = rowcreateCell(6);
newCell6setCellValue(newAssignModeequals("0")"模式1":"模式2");//设置值
if (!newAssignModeequals(assignMode)){//设置背景色
newCell6setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell6setCellStyle(hssfCellStyle);
}
if ("BRANCH"equals(newTurnableDimension)) {
newTurnableDimension = "haha";
} else if ("CENTER"equals(newTurnableDimension)) {
newTurnableDimension = "hehe";
} else if ("DEPT"equals(newTurnableDimension)) {
newTurnableDimension = "hiahia";
}
HSSFCell newCell7 = rowcreateCell(7);
newCell7setCellValue(newTurnableDimension);
if (!newTurnableDimensionequals(turnableDimension)){//设置背景色
newCell7setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell7setCellStyle(hssfCellStyle);
}
HSSFCell newCell8 = rowcreateCell(8);
newCell8setCellValue(newM0equalsIgnoreCase("Y")"案1":"案2");
if (!newM0equals(M0)){//设置背景色
newCell8setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell8setCellStyle(hssfCellStyle);
}
HSSFCell newCell9 = rowcreateCell(9);
newCell9setCellValue(newM1equalsIgnoreCase("Y")"案1":"案2");
if (!newM1equals(M1)){//设置背景色
newCell9setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell9setCellStyle(hssfCellStyle);
}
HSSFCell newCell10 = rowcreateCell(10);
newCell10setCellValue(newM2equalsIgnoreCase("Y")"案1":"案2");
if (!newM2equals(M2)){//设置背景色
newCell10setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell10setCellStyle(hssfCellStyle);
}
HSSFCell newCell11 = rowcreateCell(11);
newCell11setCellValue(newM3equalsIgnoreCase("Y")"案1":"案2");
if (!newM3equals(M3)){//设置背景色
newCell11setCellStyle(hssfCellStyleWithBackgroundColor);
}else {
newCell11setCellStyle(hssfCellStyle);
}
//合并单元格
for (int j = 0; j < 6; j++){
CellRangeAddress cellRangeAddress = new CellRangeAddress(2i+1, 2i+2, j, j);
hssfSheetaddMergedRegion(cellRangeAddress);
}
}
// 第七步,将文件输出到客户端浏览器
try {
workbookwrite(out);
outflush();
outclose();
} catch (Exception e) {
eprintStackTrace();
}
}catch(Exception e){
eprintStackTrace();
}
}
以上就是关于使用POI进行写 *** 作全部的内容,包括:使用POI进行写 *** 作、java 读取office文档、java *** 作Office办公软件(office办公软件基本 *** 作)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)