Generate an excel report in java
Use the apache poi package to implement this function.
You can download the package from the official website or through www.bkjia.com.
Next, let's look at the code.
private static String xls2003 = myexcel.xls;
Public static void main (String [] args) {// create 2003 excelString filePath = generateExcel2003 (xls2003); System. out. println (filePath + success );}
/*** Method for creating a 2003 file ** @ param filePath */public static String generateExcel2003 (String filePath) {// create a workbook object HSSFWorkbook wb = new HSSFWorkbook (); // create the Header style HSSFFont headerfont = wb. createFont (); headerfont. setFontHeightInPoints (short) 18); headerfont. setFontName (Helvetica); headerfont. setBoldweight (short) 18); HSSFCellStyle headerstyle = wb. createCellStyle (); headerstyle. setFont (headerfont); headersty Le. setAlignment (HSSFCellStyle. ALIGN_CENTER); headerstyle. setverticalignment (HSSFCellStyle. VERTICAL_CENTER); headerstyle. setWrapText (true); // create a common style HSSFFont font = wb. createFont (); font. setFontHeightInPoints (short) 12); font. setFontName (Helvetica); HSSFCellStyle cellstyle = wb. createCellStyle (); cellstyle. setFont (font); cellstyle. setAlignment (HSSFCellStyle. ALIGN_CENTER); cellstyle. setVerticalAlign Ment (HSSFCellStyle. VERTICAL_CENTER); cellstyle. setWrapText (true); // The default null style HSSFCellStyle nonstyle = wb. createCellStyle (); // create a worksheet object and name it HSSFSheet = wb. createSheet (Worksheet 1); // you can specify the width of the sheet column. setColumnWidth (0, 32*80); for (int I = 1; I <= 22; I ++) {sheet. setColumnWidth (I, 32*30);} sheet. setColumnWidth (100 *);/* ------------------ the first line ----------------------------------------- */HSSFRow row1 = sheet. createRow (0); // sets the r height. Ow1.setHeightInPoints (55); HSSFCell cell = createCell (wb, row1, 0, cellstyle, xxxxxxxxxxxxxxx); cell. setCellStyle (headerstyle); // merge cells. The first two parameters are row indexes, and the last two parameters are column indexes. addMergedRegion (new CellRangeAddress (0, 0, 0, 21);/* ---------------- second line ------------------------------------- * // wrap HSSFRow row2 = sheet. createRow (1); // time: December 2014 createCell (wb, row2, 1, cellstyle, time: December 2014); // merge the cell sheet. addMergedRegion (new CellRangeAddress (1, 1, 1, 21);/* ---------------- the third line ----------------------------------------- */HSSFRow row3 = sheet. createRow (2); row3.setHeightInPoints (30); createCell (wb, row3, 0, cellstyle, XXXX); // merge the cell sheet. addMergedRegion (new CellRangeAddress (,); createCell (wb, row3, 1, cellstyle, XXXX); sheet. addMergedRegion (new CellRangeAddress (,); createCell (wb, row3, 5, cellstyle, XXXX); sheet. addMergedRegio N (new CellRangeAddress (2, 2, 5, 8); createCell (wb, row3, 9, cellstyle, XXXX); sheet. addMergedRegion (new CellRangeAddress (2, 2, 9, 12); createCell (wb, row3, 16, cellstyle, XXXX); sheet. addMergedRegion (new CellRangeAddress (2, 2, 13, 16); createCell (wb, row3, 17, cellstyle, XXXX); sheet. addMergedRegion (new CellRangeAddress (2,2, 17,20); createCell (wb, row3, 21, cellstyle, XXXX); sheet. addMergedRegion (new CellRangeAddress (2, 3, 21,21);/* ---------------- fourth line --------------------------------------- */HSSFRow row4 = sheet. createRow (3); row4.setHeightInPoints (50); for (int I = 1; I <= 20; I ++) {switch (I % 4) {case 1: createCell (wb, row4, I, cellstyle, good); break; case 2: createCell (wb, row4, I, cellstyle, good); break; case 3: createCell (wb, row4, I, cellstyle, General); break; case 0: createCell (wb, row4, I, cellstyle, poor); break ;}} /* ---------------- the fifth line ------------------- -------------------- */HSSFRow row5 = sheet. createRow (4); row5.setHeightInPoints (50); createCell (wb, row5, 0, cellstyle, A);/* ---------------- rows 6 */HSSFRow row6 = sheet. createRow (5); row6.setHeightInPoints (50); createCell (wb, row6, 0, cellstyle, B);/* ---------------- rows 7 */HSSFRow row7 = sheet. createRow (6); row7.setHeightInPoints (50); CRES AteCell (wb, row7, 0, cellstyle, C);/* ------------------ row 8 --------------------------------------- */HSSFRow row8 = sheet. createRow (7); row8.setHeightInPoints (50); createCell (wb, row8, 0, cellstyle, total);/* ---------------- the ninth row contains */HSSFRow row9 = sheet. createRow (8); createCell (wb, row9, 0, nonstyle, remarks: 1. Scope of participants: Team members, middle-level cadres and related representatives; + 2. 184 democratic evaluation tables were issued, and 184 were withdrawn, of which 1 was blank; + Table A: team members; + Table B: middle-level cadres, 149; + C Tables: 27 others .); Sheet. addMergedRegion (new CellRangeAddress (8,);/* ------------------ generate the file ----------------------------------- */createFile (filePath, wb); return filePath ;}
Public static void createFile (String filePath, HSSFWorkbook wb) {// generate File file = new File (filePath); FileOutputStream fos = null; try {fos = new FileOutputStream (file ); wb. write (fos);} catch (Exception e) {e. printStackTrace ();} finally {if (fos! = Null) {try {fos. close () ;}catch (Exception e) {e. printStackTrace ();}}}}
/*** Creates a cell and aligns it a certain way. ** parameter * excel template * template row Reference * column subscript * cell style * cell content */private static HSSFCell createCell (HSSFWorkbook wb, HSSFRow row, int column, HSSFCellStyle cellStyle, string content) {HSSFCell cell = row. createCell (column); cell. setCellValue (content); cell. setCellStyle (cellStyle); return cell ;}