1 //創建工作薄(excel) 2 Workbook wb = new HSSFWorkbook(); 3 //創建sheet 4 Sheet createSheet = wb.createSheet("sheet1"); 5 6 //設置標題字體 7 Font fontTitle = wb.createFont(); 8 fontTitle.setFontHeightInPoints((short) 18); //字體大小 9 fontTitle.setColor(HSSFColor.BLACK.index); //字體顏色 10 fontTitle.setFontName("宋體"); //字體 11 fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗體顯示 12 //font.setItalic(true); //是否使用斜體 13 //font.setStrikeout(true); //是否使用划線 14 15 //設置標題單元格類型 16 CellStyle cellStyleTitle = wb.createCellStyle(); 17 cellStyleTitle.setFont(fontTitle); 18 cellStyleTitle.setFillForegroundColor(IndexedColors.LIME.getIndex()); 19 cellStyleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND); 20 cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中 21 cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 22 cellStyleTitle.setWrapText(true);//設置自動換行 23 24 cellStyleTitle.setBorderBottom(CellStyle.BORDER_THIN); //下邊框 25 cellStyleTitle.setBorderLeft(CellStyle.BORDER_THIN);//左邊框 26 cellStyleTitle.setBorderTop(CellStyle.BORDER_THIN);//上邊框 27 cellStyleTitle.setBorderRight(CellStyle.BORDER_THIN);//右邊框 28 cellStyleTitle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); 29 cellStyleTitle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); 30 cellStyleTitle.setTopBorderColor(IndexedColors.BLACK.getIndex()); 31 cellStyleTitle.setRightBorderColor(IndexedColors.BLACK.getIndex()); 32 33 34 //創建合並單元格 ---begin 35 CellRangeAddress region = new CellRangeAddress(0, 0, 1, 3);// 下標從0開始 起始行號,終止行號, 起始列號,終止列號 36 CellRangeAddress region2 = new CellRangeAddress(1, 2, 0, 0);// 起始行號,終止行號, 起始列號,終止列號 37 CellRangeAddress region3 = new CellRangeAddress(1, 1, 1, 3);// 起始行號,終止行號, 起始列號,終止列號 38 //在sheet里增加合並單元格 39 createSheet.addMergedRegion(region); 40 createSheet.addMergedRegion(region2); 41 createSheet.addMergedRegion(region3); 42 43 // -----------填充第一行數據------------- 44 Row rowTitle = createSheet.createRow(0); 45 Cell cellTitle = rowTitle.createCell(0); 46 cellTitle.setCellStyle(cellStyleTitle); 47 cellTitle.setCellValue("");// 設置標題內容 48 Cell cellTitle1_1 = rowTitle.createCell(1); 49 cellTitle1_1.setCellStyle(cellStyleTitle); 50 cellTitle1_1.setCellValue(dateStr);// 設置標題內容 51 // -----------填充第二行數據------------- 52 Row rowTitle1 = createSheet.createRow(1); 53 Cell cellTitle1 = rowTitle1.createCell(0); 54 cellTitle1.setCellStyle(cellStyleTitle); 55 cellTitle1.setCellValue("店名");// 設置內容 56 Cell cellTitle11 = rowTitle1.createCell(1); 57 cellTitle11.setCellStyle(cellStyleTitle); 58 cellTitle11.setCellValue("王總");// 59 Cell cellTitle111 = rowTitle1.createCell(2); 60 cellTitle111.setCellStyle(cellStyleTitle); 61 cellTitle111.setCellValue("");// 雖然這個單元格不可以不設置,但是要給它設置樣式,所以也寫了 62 Cell cellTitle1111 = rowTitle1.createCell(3); 63 cellTitle1111.setCellStyle(cellStyleTitle); 64 cellTitle1111.setCellValue("");// 雖然這個單元格不可以不設置,但是要給它設置樣式,所以也寫了 65 // -----------填充第三行數據------------- 66 Row rowTitle2 = createSheet.createRow(2); 67 Cell cellTitle2 = rowTitle2.createCell(1); 68 cellTitle2.setCellStyle(cellStyleTitle); 69 cellTitle2.setCellValue("裝修費");// 設置內容 70 Cell cellTitle22 = rowTitle2.createCell(2); 71 cellTitle22.setCellStyle(cellStyleTitle); 72 cellTitle22.setCellValue("加盟費");// 設置內容 73 Cell cellTitle222 = rowTitle2.createCell(3); 74 cellTitle222.setCellStyle(cellStyleTitle); 75 cellTitle222.setCellValue("人員培訓費");// 設置內容 76 // 合並單元格 ----end 77 // 第四行數據留着下面寫 78 79 //設置表頭字體 80 Font fontHead = wb.createFont(); 81 fontHead.setFontHeightInPoints((short) 14); //字體大小 82 fontHead.setColor(Font.COLOR_NORMAL); //字體顏色 83 fontHead.setFontName("Microsoft Sans Serif"); //字體 84 fontHead.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗體顯示 85 //font.setItalic(true); //是否使用斜體 86 //font.setStrikeout(true); //是否使用划線 87 88 //設置表頭單元格類型 89 CellStyle cellStyleHead = wb.createCellStyle(); 90 cellStyleHead.setFont(fontHead); 91 cellStyleHead.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中 92 cellStyleHead.setFillForegroundColor(IndexedColors.LIME.getIndex()); 93 cellStyleHead.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 94 cellStyleHead.setWrapText(true);//設置自動換行 95 96 cellStyleHead.setBorderBottom(CellStyle.BORDER_THIN); //下邊框 97 cellStyleHead.setBorderLeft(CellStyle.BORDER_THIN);//左邊框 98 cellStyleHead.setBorderTop(CellStyle.BORDER_THIN);//上邊框 99 cellStyleHead.setBorderRight(CellStyle.BORDER_THIN);//右邊框 100 cellStyleHead.setBottomBorderColor(IndexedColors.BLACK.getIndex()); 101 cellStyleHead.setLeftBorderColor(IndexedColors.BLACK.getIndex()); 102 cellStyleHead.setTopBorderColor(IndexedColors.BLACK.getIndex()); 103 cellStyleHead.setRightBorderColor(IndexedColors.BLACK.getIndex()); 104 105 //創建第一行,標題 106 Row row = createSheet.createRow(3); 107 String[] cellHead = {"", "","", ""}; 108 // 設置列寬 109 double[] titleWidth = {10, 24, 24, 24}; 110 for (int i = 0; i < cellHead.length; i++) { 111 Cell createCell = row.createCell(i); 112 createCell.setCellValue(cellHead[i]); 113 createCell.setCellStyle(cellStyleHead); 114 } 115 116 117 //設置內容字體 118 Font fontData = wb.createFont(); 119 fontData.setFontHeightInPoints((short) 14); //字體大小 120 fontData.setColor(Font.COLOR_NORMAL); //字體顏色 121 fontData.setFontName("Microsoft Sans Serif"); //字體 122 //font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗體顯示 123 //font.setItalic(true); //是否使用斜體 124 //font.setStrikeout(true); //是否使用划線 125 126 //設置內容單元格類型 127 CellStyle cellStyleDataOdd = wb.createCellStyle(); 128 cellStyleDataOdd.setFont(fontData); 129 cellStyleDataOdd.setFillPattern(CellStyle.SOLID_FOREGROUND); 130 cellStyleDataOdd.setAlignment(CellStyle.ALIGN_CENTER); //水平布局:居中 131 cellStyleDataOdd.setWrapText(true); 132 133 cellStyleDataOdd.setBorderBottom(CellStyle.BORDER_THIN); //下邊框 134 cellStyleDataOdd.setBorderLeft(CellStyle.BORDER_THIN);//左邊框 135 cellStyleDataOdd.setBorderTop(CellStyle.BORDER_THIN);//上邊框 136 cellStyleDataOdd.setBorderRight(CellStyle.BORDER_THIN);//右邊框 137 cellStyleDataOdd.setBottomBorderColor(IndexedColors.BLACK.getIndex()); 138 cellStyleDataOdd.setLeftBorderColor(IndexedColors.BLACK.getIndex()); 139 cellStyleDataOdd.setTopBorderColor(IndexedColors.BLACK.getIndex()); 140 cellStyleDataOdd.setRightBorderColor(IndexedColors.BLACK.getIndex()); 141 142 try { 143 int no = 1; 144 for (int i = 0; i < datas.length(); i++) { 145 JSONObject dayData = datas.getJSONObject(i); 146 String date = dayData.optString("date"); 147 JSONArray detail = dayData.getJSONArray("detail"); 148 149 for (int k = 0; k < detail.length(); k++) { 150 JSONObject ss = detail.getJSONObject(k); 151 152 row = createSheet.createRow(k + 4); 153 int j = 0; 154 // 店名 155 Cell cell1 = row.createCell(j++); 156 cell1.setCellValue(ss.optString("xxxxx")); 157 cell1.setCellStyle(cellStyleDataOdd); 158 // 裝修費 159 Cell cell2 = row.createCell(j++); 160 cell2.setCellValue(ss.optString("xxxxx")); 161 cell2.setCellStyle(cellStyleDataOdd); 162 // 加盟費 163 Cell cell7 = row.createCell(j++); 164 cell7.setCellValue(ss.optString("xxxxxx")); 165 cell7.setCellStyle(cellStyleDataOdd); 166 // 人員培訓費 167 Cell cellH = row.createCell(j++); 168 cellH.setCellValue(ss.optString("xxxxxx")); 169 cellH.setCellStyle(cellStyleDataOdd); 170 } 171 } 172 173 } catch (JSONException e) { 174 e.printStackTrace(); 175 } 176 177 // 設置列寬 178 for (int i = 0; i < titleWidth.length; i++) { 179 createSheet.setColumnWidth((short) i, (short) titleWidth[i] * 256); 180 } 181 182 // 設置上面四行凍結 183 createSheet.createFreezePane( 0, 4, 1, 4); // 前兩個參數是你要用來拆分的列數和行數。后兩個參數是下面窗口的可見象限,其中第三個參數是右邊區域可見的左邊列數,第四個參數是下面區域可見的首行 184