- package cn.com.diarc.fdev.util;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.List;
- import java.util.Map;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.hssf.util.HSSFColor;
- public class CreateExcel {
- /**
- * 在指定的位置生成 带有验证信息的 excel文件。
- * @param sheetList 将生成的excel文件的信息来源
- * @param infoMap 验证信息,将增加到excel文件里
- * @param url 上级指定的地址,在该存放生成的excel文件
- */
- public static void write(List<String[][]> sheetList, String[] sheetName, Map<String,String> infoMap, Map<String,String> updateCellMap, String url) {
- FileOutputStream fos = null;
- HSSFWorkbook wb = new HSSFWorkbook();
- //有错的cell北京设置为红
- HSSFCellStyle infoStyle = wb.createCellStyle();
- infoStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
- infoStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- //更新过的cell北京设置为黄
- HSSFCellStyle updateStyle = wb.createCellStyle();
- updateStyle.setFillForegroundColor(HSSFColor.GREEN.index);
- updateStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- //创建报告sheet
- HSSFSheet sheet_report = wb.createSheet();
- wb.setSheetName(0, "validateReport");
- int info_index = 0;
- for(int index = 0 ; index < sheetList.size() ; index ++){
- HSSFSheet sheet = wb.createSheet();
- wb.setSheetName(index + 1, sheetName[index]);
- String[][] result = sheetList.get(index);
- for(int i = 0 ; i < result.length ; i++){
- HSSFRow row = sheet.createRow(i);
- for(int j = 0 ; j < result[i].length ; j++){
- HSSFCell cell = row.createCell((short) j);
- cell.setEncoding(HSSFCell.ENCODING_UTF_16);
- String cellValue = result[i][j];
- String key = index+"_"+i+"_"+j;
- if(infoMap.containsKey(key)){
- cell.setCellStyle(infoStyle);
- HSSFRow row_info = sheet_report.createRow(info_index);
- HSSFCell cell_info1 = row_info.createCell((short) 0);
- cell_info1.setEncoding(HSSFCell.ENCODING_UTF_16);
- cell_info1.setCellValue(sheetName[index]);
- HSSFCell cell_info2 = row_info.createCell((short) 1);
- cell_info2.setEncoding(HSSFCell.ENCODING_UTF_16);
- char col_char = (char) (j + 65) ;
- String col_name = String.valueOf(col_char) + "列";
- cell_info2.setCellValue(col_name);
- HSSFCell cell_info3 = row_info.createCell((short) 2);
- cell_info3.setEncoding(HSSFCell.ENCODING_UTF_16);
- cell_info3.setCellValue(infoMap.get(key));
- info_index ++;
- }
- if(updateCellMap.containsKey(key)){
- //cell.setCellStyle(updateStyle);
- cellValue = updateCellMap.get(key);
- }
- cell.setCellValue(cellValue);
- }
- }
- }
- try {
- fos = new FileOutputStream(url);
- wb.write(fos);
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
以上是我原封不动的项目代码
接下来看看HSSF里的各类应该如何使用,以便使生成的EXCEL更帅;
- HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象
- HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象
- // Create a row and put some cells in it. Rows are 0 based.
- HSSFRow row = sheet.createRow((short)0);//建立新行
- // Create a cell and put a value in it.
- HSSFCell cell = row.createCell((short)0);//建立新cell
- cell.setCellValue(1);//设置cell的整数类型的值
- // Or do it on one line.
- row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值
- row.createCell((short)2).setCellValue("test");//设置cell字符类型的值
- row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值
- HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式
- cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式
- HSSFCell dCell =row.createCell((short)4);
- dCell.setCellValue(new Date());//设置cell为日期类型的值
- dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式
- HSSFCell csCell =row.createCell((short)5);
- csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断
- csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串
- row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell
- // Write the output to a file
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- }
- }