原創|其它|編輯:郝浩|2012-10-11 09:53:41.000|閱讀 679 次
概述:JExcel是java導出Excel的一個開源工具包。有的時候需要將數據導到Excel文件中以便觀看。本文介紹了如何使用JExcel導出Excel文件的具體做法。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
JExcel是java導出Excel的一個開源工具包。有的時候需要將數據導到Excel文件中以便觀看。
需要上網下載jxl.jar,下載地址://fc6vip.cn/zh-CN/product/1148/download.aspx
核心代碼如下:
package test; import java.io.File; import java.io.IOException; import java.util.Calendar; import java.util.Date; import jxl.Cell; import jxl.CellType; import jxl.DateCell; import jxl.LabelCell; import jxl.NumberCell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.DateFormat; import jxl.write.DateTime; import jxl.write.Label; import jxl.write.Number; import jxl.write.NumberFormat; import jxl.write.NumberFormats; import jxl.write.WritableCell; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class TutorialMain { /** * WritingSpreadSheet */ public void WritingSpreadSheet() { try { //創建一個WorkBook,即一個Excel文件,文件目錄為param0 WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\\output.xls")); //創建一個Sheet,即表單,名字為param0;位置為param1 WritableSheet sheet = workbook.createSheet("First Sheet Name", 0); sheet.setColumnView(0, 30); // 設置列的寬度 sheet.setColumnView(1, 30); // 設置列的寬度 sheet.setColumnView(2, 30); // 設置列的寬度 sheet.setRowView(6, 1000); // 設置行的高度 sheet.setRowView(4, 1000); // 設置行的高度 sheet.setRowView(5, 1000); // 設置行的高度 //創建一個Label,x坐標param0,y坐標param1,名字為param2 Label label = new Label(0, 2, "A label record"); //將該Label sheet.addCell(label); //創建一個Number,x坐標param0,y坐標param1,數值為param2 Number number = new Number(3, 4, 3.1459); sheet.addCell(number); /** * 格式化信息 */ // Create a cell format for Arial 10 point font WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); WritableCellFormat arial10format = new WritableCellFormat (arial10font); // Create the label, specifying content and format Label label2 = new Label(1,0, "Arial 10 point label", arial10format); sheet.addCell(label2); Label label3 = new Label(2, 0, "Another Arial 10 point label", arial10format); sheet.addCell(label3); // Create a cell format for Times 16, bold and italic WritableFont times16font = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true); WritableCellFormat times16format = new WritableCellFormat (times16font); // Create the label, specifying content and format Label label4 = new Label(3,0, "Times 16 bold italic label", times16format); times16format.setBackground(jxl.format.Colour.BLUE); times16format.setAlignment(jxl.format.Alignment.CENTRE); sheet.addCell(label4); /** * 格式化數值 */ WritableCellFormat integerFormat = new WritableCellFormat (NumberFormats.INTEGER); Number number2 = new Number(0, 4, 3.141519, integerFormat); sheet.addCell(number2); WritableCellFormat floatFormat = new WritableCellFormat (NumberFormats.FLOAT); Number number3 = new Number(1, 4, 3.141519, floatFormat); sheet.addCell(number3); NumberFormat fivedps = new NumberFormat("#.#####"); WritableCellFormat fivedpsFormat = new WritableCellFormat(fivedps); Number number4 = new Number(2, 4, 3.141519, fivedpsFormat); sheet.addCell(number4); WritableCellFormat fivedpsFontFormat = new WritableCellFormat (times16font, fivedps); Number number5 = new Number(3, 4, 3.141519, fivedpsFontFormat); sheet.addCell(number5); /** * Formatting Dates */ // Get the current date and time from the Calendar object Date now = Calendar.getInstance().getTime(); DateFormat customDateFormat = new DateFormat ("dd MM yyyy hh:mm:ss"); WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat); DateTime dateCell = new DateTime(0, 6, now, dateFormat); sheet.addCell(dateCell); /** * 合并單元格 */ WritableSheet sheet1 = workbook.createSheet("First Sheet", 1); sheet1.mergeCells(0, 0, 1, 1); // All sheets and cells added. Now write out the workbook workbook.write(); workbook.close(); System.out.println("創建成功.."); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("創建失敗.."); e.printStackTrace(); } catch (RowsExceededException e) { // sheet.addCell Exception System.out.println("創建失敗.."); e.printStackTrace(); } catch (WriteException e) { System.out.println("創建失敗.."); // TODO Auto-generated catch block e.printStackTrace(); } } /** * * @param filePath 文件路徑 * @param fileName 文件名 */ public void ReadingSpreadSheets(String filePath,String fileName){ try { Workbook workbook = Workbook.getWorkbook(new File(filePath + "\\" + fileName)); Sheet sheet = workbook.getSheet(0); Cell a1 = sheet.getCell(0,0); Cell b2 = sheet.getCell(1,1); Cell c2 = sheet.getCell(2,1); String stringa1 = a1.getContents(); String stringb2 = b2.getContents(); String stringc2 = c2.getContents(); double numberb2 = 0; Date datec2 = null; if (a1.getType() == CellType.LABEL) { LabelCell lc = (LabelCell) a1; stringa1 = lc.getString(); } if (b2.getType() == CellType.NUMBER) { NumberCell nc = (NumberCell) b2; numberb2 = nc.getValue(); } if (c2.getType() == CellType.DATE) { DateCell dc = (DateCell) c2; datec2 = dc.getDate(); } // 其他處理 // ... // 結束后關閉文件并釋放內存 workbook.close(); System.out.println("讀取成功.."); } catch (BiffException e) { // TODO Auto-generated catch block System.out.println("讀取失敗.."); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("讀取失敗.."); e.printStackTrace(); } } public void copyWorkBook(String oldFilePath,String oldFileName,String newFilePath,String newFileName ){ try { Workbook workbook = Workbook.getWorkbook(new File( oldFilePath + "\\" + oldFileName )); WritableWorkbook copy = Workbook.createWorkbook(new File( newFilePath +"\\" +newFileName ), workbook); //進行一些操作 WritableSheet sheet = copy.getSheet(1); WritableCell cell0 = sheet.getWritableCell(1, 2); if (cell0.getType() == CellType.LABEL) { Label l0 = (Label) cell0; l0.setString("modified cell"); } WritableSheet sheet2 = copy.getSheet(1); WritableCell cell = sheet2.getWritableCell(2, 4); NumberFormat fivedps = new NumberFormat("#.#####"); WritableCellFormat cellFormat = new WritableCellFormat(fivedps); cell.setCellFormat(cellFormat); Label label = new Label(0, 2, "New label record"); sheet2.addCell(label); Number number = new Number(3, 4, 3.1459); sheet2.addCell(number); // 所有的都完成,關閉資源 workbook.close(); copy.write(); copy.close(); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args){ TutorialMain main = new TutorialMain(); main.WritingSpreadSheet(); } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:kenchow126的專欄