1. 程式人生 > >java中對Excel的建立、樣式修改

java中對Excel的建立、樣式修改

建立excel檔案

public static String createExcel(ArrayList<String> list, ArrayList<ArrayList<String>> DataList, String dateStr) {
        // 建立一個excel檔案
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 建立一個excel的sheet(表)
        HSSFSheet sheet = workbook.createSheet();

        
// 建立第一行 HSSFRow row0 = sheet.createRow(0); // 設定首行行高 Row row = sheet.getRow(0); row.setHeightInPoints(30); // 建立第一列 HSSFCell cellTitle = row0.createCell(0); // 合併單元格 CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 4 + (list.size() - 5) * 2);//
起始行號,終止行號, // 起始列號,終止列號 sheet.addMergedRegion(cellRangeAddress); int j = 5; for (int i = 0; i < list.size() - 5; i++) { CellRangeAddress cellRangeAddress1 = new CellRangeAddress(1, 1, j + i, j + 1 + i); sheet.addMergedRegion(cellRangeAddress1); j
++; } for (int i = 0; i < 5; i++) { CellRangeAddress cellRangeAddress3 = new CellRangeAddress(1, 2, i, i); sheet.addMergedRegion(cellRangeAddress3); } // 給第一個單元格賦值(標題行) cellTitle.setCellValue(dateStr + "考勤簽到記錄表"); cellTitle.setCellStyle(getRow0Style(workbook)); // 建立第二行(列名行) int t = 1; HSSFRow row1 = sheet.createRow(1); row1.setHeightInPoints(20); for (int i = 0; i < list.size(); i++) { HSSFCell cell1 = null; // 迴圈建立第二行各列 if (i > 5) { cell1 = row1.createCell(i + t); t++; } else { cell1 = row1.createCell(i); } // 設定單元格資料型別 cell1.setCellType(HSSFCell.CELL_TYPE_STRING); HSSFRichTextString text = null; if (i < list.size()) { text = new HSSFRichTextString(list.get(i)); // 給每列賦值 // System.out.println(text+"____"+i+"_________"+list.size()); cell1.setCellValue(text); } } // 給單元格新增樣式 for (int i = 0; i <= 4 + (list.size() - 5) * 2; i++) { HSSFCell cell = row1.getCell(i); if (cell == null) { cell = row1.createCell(i); cell.setCellValue(""); } cell.setCellStyle(getStyle(workbook)); } // 建立第三行(早上下午) HSSFRow row3 = sheet.createRow(2); row3.setHeightInPoints(15); for (int i = 5; i <= 4 + (list.size() - 5) * 2; i++) { HSSFCell cell = row3.createCell(i); // 設定單元格資料型別 cell.setCellType(HSSFCell.CELL_TYPE_STRING); if (i % 2 == 1) { cell.setCellValue("早"); } else { cell.setCellValue("晚"); } } // 給單元格新增樣式 for (int i = 0; i <= 4 + (list.size() - 5) * 2; i++) { HSSFCell cell = row3.getCell(i); if (cell == null) { cell = row3.createCell(i); cell.setCellValue(""); } cell.setCellStyle(getStyle3(workbook)); } // 建立資料行 for (int k = 0; k < DataList.size(); k++) { HSSFRow row2 = sheet.createRow(3 + k); ArrayList<String> datArr = DataList.get(k); row2.setHeightInPoints(20); for (int i = 0; i < datArr.size(); i++) { // 迴圈建立第三行各列 HSSFCell cell2 = row2.createCell(i); // 設定單元格樣式 cell2.setCellStyle(getStyle(workbook)); // 給每一列賦值 String str = datArr.get(i); cell2.setCellValue(str); } } sheet.autoSizeColumn((short) 0); // 調整第一列寬度 sheet.autoSizeColumn((short) 1); // 調整第二列寬度 sheet.autoSizeColumn((short) 2); // 調整第三列寬度 try { // 建立輸出流 File file = new File("C:\\Work\\WeX5_3.8_BSBZB\\model\\UI2\\excel\\" + dateStr + ".xls");// official伺服器path // File file = new // File("F:\\Wex5Project\\WeX5_3.8_1_beiSong\\model\\UI2\\excel\\" + dateStr + // ".xls");//my path FileOutputStream fileout = new FileOutputStream(file); // 將excel寫入輸出流 workbook.write(fileout); fileout.flush(); fileout.close(); System.out.println("建立完成!"); } catch (Exception e) { e.printStackTrace(); } // String fiel = "http://192.168.1.156:8181/x5/UI2/excel/" + dateStr + // ".xls";//my path /*official伺服器path北送*/ String fiel = "http://111.198.48.184:8888/x5/UI2/excel/" + dateStr + ".xls"; return fiel; }