java生成excel又一篇

 
  1. package cn.com.diarc.fdev.util;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.poi.hssf.usermodel.HSSFCell;
  7. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  8. import org.apache.poi.hssf.usermodel.HSSFRow;
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import org.apache.poi.hssf.util.HSSFColor;
  12. public class CreateExcel {
  13.     /**
  14.      * 在指定的位置生成 带有验证信息的 excel文件。
  15.      * @param sheetList 将生成的excel文件的信息来源
  16.      * @param infoMap 验证信息,将增加到excel文件里
  17.      * @param url 上级指定的地址,在该存放生成的excel文件
  18.      */
  19.     public static void write(List<String[][]> sheetList, String[] sheetName, Map<String,String> infoMap, Map<String,String> updateCellMap, String url) {
  20.         FileOutputStream fos = null;
  21.         HSSFWorkbook wb = new HSSFWorkbook();
  22.         //有错的cell北京设置为红
  23.         HSSFCellStyle infoStyle = wb.createCellStyle();
  24.         infoStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
  25.         infoStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  26.         //更新过的cell北京设置为黄
  27.         HSSFCellStyle updateStyle = wb.createCellStyle();
  28.         updateStyle.setFillForegroundColor(HSSFColor.GREEN.index);
  29.         updateStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  30.         //创建报告sheet
  31.         HSSFSheet sheet_report = wb.createSheet();
  32.         wb.setSheetName(0"validateReport");
  33.         int info_index = 0;
  34.         for(int index = 0 ; index < sheetList.size() ; index ++){
  35.             HSSFSheet sheet = wb.createSheet();
  36.             wb.setSheetName(index + 1, sheetName[index]);
  37.             String[][] result = sheetList.get(index);
  38.             for(int i = 0 ; i < result.length ; i++){
  39.                 HSSFRow row = sheet.createRow(i);
  40.                 for(int j = 0  ; j < result[i].length ; j++){
  41.                     HSSFCell cell = row.createCell((short) j);
  42.                     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  43.                     String cellValue = result[i][j];
  44.                     String key = index+"_"+i+"_"+j;
  45.                     if(infoMap.containsKey(key)){
  46.                         cell.setCellStyle(infoStyle);
  47.                         
  48.                         HSSFRow row_info = sheet_report.createRow(info_index);
  49.                         HSSFCell cell_info1 = row_info.createCell((short0);
  50.                         cell_info1.setEncoding(HSSFCell.ENCODING_UTF_16);
  51.                         cell_info1.setCellValue(sheetName[index]); 
  52.                         
  53.                         HSSFCell cell_info2 = row_info.createCell((short1);
  54.                         cell_info2.setEncoding(HSSFCell.ENCODING_UTF_16);
  55.                         char col_char = (char) (j + 65) ;
  56.                         String col_name = String.valueOf(col_char) + "列";
  57.                         cell_info2.setCellValue(col_name); 
  58.                         
  59.                         HSSFCell cell_info3 = row_info.createCell((short2);
  60.                         cell_info3.setEncoding(HSSFCell.ENCODING_UTF_16);
  61.                         cell_info3.setCellValue(infoMap.get(key)); 
  62.                         
  63.                         info_index ++;
  64.                     }
  65.                     if(updateCellMap.containsKey(key)){
  66.                         //cell.setCellStyle(updateStyle);
  67.                         cellValue = updateCellMap.get(key);
  68.                     }
  69.                     cell.setCellValue(cellValue);
  70.                 }
  71.             }
  72.         }
  73.         try {
  74.             fos = new FileOutputStream(url);
  75.             wb.write(fos);
  76.             fos.close();
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81. }

以上是我原封不动的项目代码

接下来看看HSSF里的各类应该如何使用,以便使生成的EXCEL更帅;

  1. HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象
  2. HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象
  3. // Create a row and put some cells in it. Rows are 0 based.
  4. HSSFRow row = sheet.createRow((short)0);//建立新行
  5. // Create a cell and put a value in it.
  6. HSSFCell cell = row.createCell((short)0);//建立新cell
  7. cell.setCellValue(1);//设置cell的整数类型的值
  8. // Or do it on one line.
  9. row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值
  10. row.createCell((short)2).setCellValue("test");//设置cell字符类型的值
  11. row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值
  12. HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式
  13. cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式
  14. HSSFCell dCell =row.createCell((short)4);
  15. dCell.setCellValue(new Date());//设置cell为日期类型的值
  16. dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式
  17. HSSFCell csCell =row.createCell((short)5);
  18. csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断
  19. csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串
  20. row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell
  21. // Write the output to a file
  22. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  23. wb.write(fileOut);
  24. fileOut.close();
  25. }
  26. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值