jxl生成表格(合并单元格,字体,样式)
生活随笔
收集整理的這篇文章主要介紹了
jxl生成表格(合并单元格,字体,样式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public static String testEx() {// a, 計算路徑SimpleDateFormat sdf = new SimpleDateFormat("/yyyy-MM-dd/");String datePath = sdf.format(new Date());datePath = "中國石油西部管道輸氣量交接電子文檔" + datePath;String uuidFileName = UUID.randomUUID().toString();// 創建文件夾File dir = new File(Globals.fielImportPath + datePath);if (!dir.exists()) {dir.mkdirs();}String path = Globals.fielImportPath + datePath + uuidFileName + "."+ "xls";try {WritableWorkbook wwb = Workbook.createWorkbook(new File(path));WritableSheet sheet1= wwb.createSheet("前五個站隊", 0);WritableSheet sheet2 = wwb.createSheet("后兩個站隊", 1);WritableSheet sheet3 = wwb.createSheet("后三個站隊", 2);sheet1.setColumnView(0, 30); // 設置列的寬度sheet1.setColumnView(1, 30); // 設置列的寬度sheet1.setColumnView(2, 30); // 設置列的寬度sheet1.setColumnView(3, 30); // 設置列的寬度sheet1.setColumnView(4, 30); // 設置列的寬度sheet1.setColumnView(5, 30); // 設置列的寬度sheet1.setRowView(0, 1000); // 設置行的高度sheet1.setRowView(1, 500); // 設置行的高度/*** 定義單元格樣式*/WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 20,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED); // 定義格式 字體 下劃線 斜體 粗體 顏色WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN); // 定義格式 字體 下劃線 斜體 粗體 顏色WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 8,WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定義格式 字體 下劃線 斜體 粗體 顏色WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 單元格定義wcf_title.setBackground(jxl.format.Colour.BLACK); // 設置單元格的背景顏色wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 設置對齊方式WritableCellFormat wcf_head = new WritableCellFormat(wf_head); wcf_head.setBackground(jxl.format.Colour.BLACK);wcf_head.setAlignment(jxl.format.Alignment.CENTRE); WritableCellFormat wcf_table = new WritableCellFormat(wf_table); wcf_table.setBackground(jxl.format.Colour.BLACK); wcf_table.setAlignment(jxl.format.Alignment.CENTRE); /*** 使用樣式的單元格*/// 1.添加Label對象三個參數意思:【列,行,值】sheet1.addCell(new Label(0, 0, "標題", wcf_title)); // 普通的帶有定義格式的單元格sheet1.addCell(new Label(0, 1, "表頭1", wcf_head));sheet1.addCell(new Label(1, 1, "表頭2", wcf_head));sheet1.addCell(new Label(2, 1, "表頭3", wcf_head));sheet1.addCell(new Label(3, 1, "表頭4", wcf_head));sheet1.addCell(new Label(4, 1, "表頭5", wcf_head));sheet1.addCell(new Label(5, 1, "表頭6", wcf_head));sheet1.mergeCells(0, 0, 5, 0); // 合并單元格// 寫入Exel工作表wwb.write();// 關閉Excel工作薄對象wwb.close();} catch (Exception e) {e.printStackTrace();return "0";}return path;}public static void main(String[] args) {UtilIO uio=new UtilIO();System.out.println(uio.testEx());}
預覽效果
?
?
總結
以上是生活随笔為你收集整理的jxl生成表格(合并单元格,字体,样式)的全部內容,希望文章能夠幫你解決所遇到的問題。