POI操作xls表格文件
程序员文章站
2022-07-13 13:11:06
...
private void printExcelList(String templateFile, String writeFile,
int beginRowIndex, String[] columns, boolean needIndex,
List<Map<String, Object>> objectList) throws Exception {
if(objectList != null && objectList.size() > 0
&& columns != null && columns.length > 0
&& templateFile != null && !templateFile.equals("")
&& writeFile != null && !writeFile.equals("")){
FileInputStream in = null;
//先做输入文件的格式检查
try{
in = new FileInputStream(new File(templateFile));
}catch(Exception e){
throw new Exception("模板文件“" + templateFile + "”找不到!");
}
//做输出文件的格式检查
String path = "";
if(writeFile.lastIndexOf("\\") >= 0){
path = writeFile.substring(0,writeFile.lastIndexOf("\\"));
}else if(writeFile.lastIndexOf("/") >= 0){
path = writeFile.substring(0,writeFile.lastIndexOf("/"));
}else{
throw new Exception("输出文件“" + writeFile + "”格式不正确!");
}
File pathFile = new File(path);
if(!pathFile.exists()){
pathFile.mkdirs();
}
FileOutputStream fileOut = new FileOutputStream(writeFile);
HSSFWorkbook hwb = new HSSFWorkbook(in);
HSSFCellStyle style1 = hwb.createCellStyle();
style1.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
HSSFCellStyle style2 = hwb.createCellStyle();
HSSFFont font=hwb.createFont();
font.setFontHeightInPoints((short)12);
font.setFontName("宋体");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style2.setFont(font);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
HSSFCellStyle style3 = hwb.createCellStyle();
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style3.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style3.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style3.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
HSSFSheet sheet = hwb.getSheetAt(0);
sheet.setDefaultRowHeight((short)540);
int sn = 0;
for(int i = 0; i < objectList.size(); i++) {
Map<String,Object> map = objectList.get(sn);
HSSFRow row;
row = sheet.createRow(i + beginRowIndex);
row.setHeight((short)540);
HSSFCell cell = null;
int startCellNum = 0;
//如果需要添加序号一列,则将序号一列置于第一列,后面的内容值依次往后面移一个单元格
if(needIndex){
startCellNum = 1;
cell = row.createCell(0);
cell.setCellValue(i+1);
}
for(int index = 0; index < columns.length; index++){
cell = row.createCell(index + startCellNum);
if(index==0){
cell.setCellStyle(style1);
}else if(index==2){
cell.setCellStyle(style2);
}else{
cell.setCellStyle(style3);
}
cell.setCellValue(map.get(columns[index]) == null ? "" : map.get(columns[index]).toString());
}
sn ++;
}
hwb.write(fileOut);
fileOut.flush();
fileOut.close();
in.close();
}else{
throw new Exception("传入主要参数不能有空");
}
推荐阅读
-
Python基于matplotlib画箱体图检验异常值操作示例【附xls数据文件下载】
-
PHP5.6读写excel表格文件操作示例
-
结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程
-
BootStrap Fileinput插件和Bootstrap table表格插件相结合实现文件上传、预览、提交的导入Excel数据操作步骤
-
Java操作Jxl实现导出数据生成Excel表格数据文件
-
Java使用POI操作Excel表格上传下载
-
使用POI同时对Excel文件进行读和写操作时避免Invalid header signatu
-
POI 通用 读取excle xls xlsx 文件
-
Python中的有关CSV文件和XLSX表格文件的操作
-
poi 文件导出 xls