java poi 模板 导出_POI通过模板导出EXCEL文件

1 packagecom.pole.educate.excel;2 importjava.io.File;3 importjava.io.FileInputStream;4 importjava.io.FileNotFoundException;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.util.Calendar;8 importjava.util.Date;9

10 importorg.apache.poi.hssf.usermodel.HSSFCell;11 importorg.apache.poi.hssf.usermodel.HSSFCellStyle;12 importorg.apache.poi.hssf.usermodel.HSSFRow;13 importorg.apache.poi.hssf.usermodel.HSSFSheet;14 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;15 importorg.apache.poi.hssf.util.HSSFColor;16 importorg.apache.poi.poifs.filesystem.POIFSFileSystem;17 importorg.apache.poi.ss.usermodel.RichTextString;18 /**

19 * 共分为六部完成根据模板导出excel操作:
20 * 第一步、设置excel模板路径(setSrcPath)
21 * 第二步、设置要生成excel文件路径(setDesPath)
22 * 第三步、设置模板中哪个Sheet列(setSheetName)
23 * 第四步、获取所读取excel模板的对象(getSheet)
24 * 第五步、设置数据(分为6种类型数据:setCellStrValue、setCellDateValue、setCellDoubleValue、setCellBoolValue、setCellCalendarValue、setCellRichTextStrValue)
25 * 第六步、完成导出 (exportToNewFile)
26 *27 *@authorAdministrator28 *29 */

30 public classExcelWriter {31 POIFSFileSystem fs = null;32 HSSFWorkbook wb = null;33 HSSFSheet sheet = null;34 HSSFCellStyle cellStyle = null;35

36 private String srcXlsPath = "";//excel模板路径

37 private String desXlsPath = ""; //生成路径

38 private String sheetName = "";39

40 /**

41 * 第一步、设置excel模板路径42 *@paramsrcXlsPaths43 */

44 public voidsetSrcPath(String srcXlsPaths) {45 this.srcXlsPath =srcXlsPaths;46 }47

48 /**

49 * 第二步、设置要生成excel文件路径50 *@paramdesXlsPaths51 *@throwsFileNotFoundException52 */

53 public void setDesPath(String desXlsPaths) throwsFileNotFoundException {54 this.desXlsPath =desXlsPaths;55 }56

57 /**

58 * 第三步、设置模板中哪个Sheet列59 *@paramsheetName60 */

61 public voidsetSheetName(String sheetName) {62 this.sheetName =sheetName;63 }64

65 /**

66 * 第四步、获取所读取excel模板的对象67 */

68 public voidgetSheet() {69 try{70 File fi = newFile(srcXlsPath);71 if(!fi.exists()){72 //System.out.println("模板文件:"+srcXlsPath+"不存在!");

73 return;74 }75 fs = new POIFSFileSystem(newFileInputStream(fi));76 wb = newHSSFWorkbook(fs);77 sheet =wb.getSheet(sheetName);78

79 //生成单元格样式

80 cellStyle =wb.createCellStyle();81 //设置背景颜色

82 cellStyle.setFillForegroundColor(HSSFColor.RED.index);83 //solid 填充 foreground 前景色

84 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);85

86 } catch(FileNotFoundException e) {87 e.printStackTrace();88 } catch(IOException e) {89 e.printStackTrace();90 }91 }92 /**

93 *94 */

95 public HSSFRow createRow(introwIndex) {96 HSSFRow row =sheet.createRow(rowIndex);97 returnrow;98 }99 /**

100 *101 */

102 public void createCell(HSSFRow row,intcolIndex) {103 row.createCell(colIndex);104 }105 /**

106 * 第五步、设置单元格的样式107 *@paramrowIndex 行值108 *@paramcellnum 列值109 */

110 public void setCellStyle(int rowIndex, intcellnum) {111 HSSFCell cell =sheet.getRow(rowIndex).getCell(cellnum);112 cell.setCellStyle(cellStyle);113 }114

115 /**

116 * 第五步、设置字符串类型的数据117 *@paramrowIndex 行值118 *@paramcellnum 列值119 *@paramvalue 字符串类型的数据120 */

121 public void setCellStrValue(int rowIndex, intcellnum, String value) {122 if(value != null) {123 HSSFCell cell =sheet.getRow(rowIndex).getCell(cellnum);124 cell.setCellValue(value);125 }126 }127

128 /**

129 * 第五步、设置日期/时间类型的数据130 *@paramrowIndex 行值131 *@paramcellnum 列值132 *@paramvalue 日期/时间类型的数据133 */

134 public void setCellDateValue(int rowIndex, intcellnum, Date value) {135 HSSFCell cell =sheet.getRow(rowIndex).getCell(cellnum);136 cell.setCellValue(value);137 }138

139 /**

140 * 第五步、设置浮点类型的数据141 *@paramrowIndex 行值142 *@paramcellnum 列值143 *@paramvalue 浮点类型的数据144 */

145 public void setCellDoubleValue(int rowIndex, int cellnum, doublevalue) {146 HSSFCell cell =sheet.getRow(rowIndex).getCell(cellnum);147 cell.setCellValue(value);148 }149

150 /**

151 * 第五步、设置Bool类型的数据152 *@paramrowIndex 行值153 *@paramcellnum 列值154 *@paramvalue Bool类型的数据155 */

156 public void setCellBoolValue(int rowIndex, int cellnum, booleanvalue) {157 HSSFCell cell =sheet.getRow(rowIndex).getCell(cellnum);158 cell.setCellValue(value);159 }160

161 /**

162 * 第五步、设置日历类型的数据163 *@paramrowIndex 行值164 *@paramcellnum 列值165 *@paramvalue 日历类型的数据166 */

167 public void setCellCalendarValue(int rowIndex, intcellnum, Calendar value) {168 HSSFCell cell =sheet.getRow(rowIndex).getCell(cellnum);169 cell.setCellValue(value);170 }171

172 /**

173 * 第五步、设置富文本字符串类型的数据。可以为同一个单元格内的字符串的不同部分设置不同的字体、颜色、下划线174 *@paramrowIndex 行值175 *@paramcellnum 列值176 *@paramvalue 富文本字符串类型的数据177 */

178 public void setCellRichTextStrValue(int rowIndex, intcellnum,179 RichTextString value) {180 HSSFCell cell =sheet.getRow(rowIndex).getCell(cellnum);181 cell.setCellValue(value);182 }183

184 /**

185 * 第六步、完成导出186 */

187 public voidexportToNewFile() {188 FileOutputStream out;189 try{190 out = newFileOutputStream(desXlsPath);191 wb.write(out);192 out.close();193 } catch(FileNotFoundException e) {194 e.printStackTrace();195 } catch(IOException e) {196 e.printStackTrace();197 }198 }199

200 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值