- package excel;
- import java.io.FileOutputStream;
- import java.io.OutputStream;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import jxl.Workbook;
- import jxl.format.Alignment;
- import jxl.format.Border;
- import jxl.format.BorderLineStyle;
- import jxl.format.CellFormat;
- import jxl.write.Label;
- import jxl.write.WritableCellFormat;
- import jxl.write.WritableFont;
- import jxl.write.WritableSheet;
- import jxl.write.WritableWorkbook;
- /***********************************************************************
- *
- * jxlCreate.java
- * @copyright Copyright: 2009-2012
- * @creator 周輝<br/>
- * @create-time Mar 9, 2010 1:35:19 PM
- * @revision $Id: *
- ***********************************************************************/
- public class jxlCreate {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 準備設置excel工作表的標題
- String[] title = {"編號","產品名稱","產品價格","產品數量","生產日期","產地","是否出口"};
- try {
- // 獲得開始時間
- long start = System.currentTimeMillis();
- // 輸出的excel的路徑
- String filePath = "c://test.xls";
- // 創建Excel工作薄
- WritableWorkbook wwb;
- // 新建立一個jxl文件,即在C盤下生成test.xls
- OutputStream os = new FileOutputStream(filePath);
- wwb=Workbook.createWorkbook(os);
- // 添加第一個工作表並設置第一個Sheet的名字
- WritableSheet sheet = wwb.createSheet("產品清單", 0);
- Label label;
- for(int i=0;i<title.length;i++){
- // Label(x,y,z)其中x代表單元格的第x+1列,第y+1行, 單元格的內容是y
- // 在Label對象的子對象中指明單元格的位置和內容
- label = new Label(i,0,title[i]);
- // 將定義好的單元格添加到工作表中
- sheet.addCell(label);
- }
- // 下面是填充數據
- /*
- * 保存數字到單元格,需要使用jxl.write.Number
- * 必須使用其完整路徑,否則會出現錯誤
- * */
- // 填充產品編號
- jxl.write.Number number = new jxl.write.Number(0,1,20071001);
- sheet.addCell(number);
- // 填充產品名稱
- label = new Label(1,1,"金鴿瓜子");
- sheet.addCell(label);
- /*
- * 定義對於顯示金額的公共格式
- * jxl會自動實現四捨五入
- * 例如 2.456會被格式化爲2.46,2.454會被格式化爲2.45
- * */
- jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##");
- jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat(nf);
- // 填充產品價格
- jxl.write.Number nb = new jxl.write.Number(2,1,2.45,wcf);
- sheet.addCell(nb);
- // 填充產品數量
- jxl.write.Number numb = new jxl.write.Number(3,1,200);
- sheet.addCell(numb);
- /*
- * 定義顯示日期的公共格式
- * 如:yyyy-MM-dd hh:mm
- * */
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String newdate = sdf.format(new Date());
- // 填充出產日期
- label = new Label(4,1,newdate);
- sheet.addCell(label);
- // 填充產地
- label = new Label(5,1,"陝西西安");
- sheet.addCell(label);
- /*
- * 顯示布爾值
- * */
- jxl.write.Boolean bool = new jxl.write.Boolean(6,1,true);
- sheet.addCell(bool);
- /*
- * 合併單元格
- * 通過writablesheet.mergeCells(int x,int y,int m,int n);來實現的
- * 表示將從第x+1列,y+1行到m+1列,n+1行合併
- *
- * */
- sheet.mergeCells(0,3,2,3);
- label = new Label(0,3,"合併了三個單元格");
- sheet.addCell(label);
- /*
- *
- * 定義公共字體格式
- * 通過獲取一個字體的樣式來作爲模板
- * 首先通過web.getSheet(0)獲得第一個sheet
- * 然後取得第一個sheet的第二列,第一行也就是"產品名稱"的字體
- * */
- CellFormat cf = wwb.getSheet(0).getCell(1, 0).getCellFormat();
- WritableCellFormat wc = new WritableCellFormat();
- // 設置居中
- wc.setAlignment(Alignment.CENTRE);
- // 設置邊框線
- wc.setBorder(Border.ALL, BorderLineStyle.THIN);
- // 設置單元格的背景顏色
- wc.setBackground(jxl.format.Colour.RED);
- label = new Label(1,5,"字體",wc);
- sheet.addCell(label);
- // 設置字體
- jxl.write.WritableFont wfont = new jxl.write.WritableFont(WritableFont.createFont("隸書"),20);
- WritableCellFormat font = new WritableCellFormat(wfont);
- label = new Label(2,6,"隸書",font);
- sheet.addCell(label);
- // 寫入數據
- wwb.write();
- // 關閉文件
- wwb.close();
- long end = System.currentTimeMillis();
- System.out.println("----完成該操作共用的時間是:"+(end-start)/1000);
- } catch (Exception e) {
- System.out.println("---出現異常---");
- e.printStackTrace();
- }
- }
- }
2.POI 生成
- package excel;
- import java.io.FileOutputStream;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import org.apache.poi.ss.usermodel.DataFormat;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.ss.util.CellRangeAddress;
- /***********************************************************************
- *
- * poiCreate.java
- * @copyright Copyright: 2009-2012
- * @creator 周輝<br/>
- * @create-time Mar 9, 2010 2:27:52 PM
- * @revision $Id: *
- ***********************************************************************/
- public class poiCreate {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- //創建一個EXCEL
- Workbook wb = new HSSFWorkbook();
- DataFormat format = wb.createDataFormat();
- CellStyle style;
- //創建一個SHEET
- Sheet sheet1 = wb.createSheet("產品清單");
- String[] title = {"編號","產品名稱","產品價格","產品數量","生產日期","產地","是否出口"};
- int i=0;
- //創建一行
- Row row = sheet1.createRow((short)0);
- //填充標題
- for (String s:title){
- Cell cell = row.createCell(i);
- cell.setCellValue(s);
- i++;
- }
- Row row1 = sheet1.createRow((short)1);
- //下面是填充數據
- row1.createCell(0).setCellValue(20071001);
- row1.createCell(1).setCellValue("金鴿瓜子");
- //創建一個單元格子
- Cell cell2=row1.createCell(2);
- // 填充產品價格
- cell2.setCellValue(2.45);
- style = wb.createCellStyle();
- style.setDataFormat(format.getFormat("#.##"));
- //設定樣式
- cell2.setCellStyle(style);
- // 填充產品數量
- row1.createCell(3).setCellValue(200);
- /*
- * 定義顯示日期的公共格式
- * 如:yyyy-MM-dd hh:mm
- * */
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String newdate = sdf.format(new Date());
- // 填充出產日期
- row1.createCell(4).setCellValue(newdate);
- row1.createCell(5).setCellValue("陝西西安");
- /*
- * 顯示布爾值
- * */
- row1.createCell(6).setCellValue(true);
- /*
- * 合併單元格
- * 通過writablesheet.mergeCells(int x,int y,int m,int n);來實現的
- * 表示將first row, last row,first column,last column
- *
- * */
- Row row2 = sheet1.createRow((short) 2);
- Cell cell3 = row2.createCell((short) 0);
- cell3.setCellValue("合併了三個單元格");
- sheet1.addMergedRegion(new CellRangeAddress(2,2,0,2));
- FileOutputStream fileOut = new FileOutputStream("d://test.xls");
- wb.write(fileOut);
- fileOut.close();
- }
- }
上面代碼2中方式生成 2張報表,涉及到基本生成報表中的幾種單元格類型。
POI 用的JAR poi-3.6-20091214.jar jxl 用到的jar jxl-2.6.jar
2 種方式都相對比較好用,個人推薦使用POI (apache的項目)
相關參考資料可以去官方網站查看
http://poi.apache.org/spreadsheet/quick-guide.html