標籤:電話 下載 login div gen images format color 技術分享
要做一個後台自動化,要先預先產生一份文檔,以下內容產生了文檔首先下載jxl.jar包,:http://download.csdn.net/detail/prstaxy/44699351.產生一個簡單的excel文檔
WritableWorkbook workbook = null; try { workbook = Workbook.createWorkbook(new File("F:\\test.xls")); //建立新的一頁 WritableSheet sheet = workbook.createSheet("First Sheet", 0); //建立要顯示的具體內容 Label formate = new Label(0,0,"資料格式"); sheet.addCell(formate); Label floats = new Label(1,0,"浮點型"); sheet.addCell(floats); Label integers = new Label(2,0,"整型"); sheet.addCell(integers); Label booleans = new Label(3,0,"布爾型"); sheet.addCell(booleans); Label dates = new Label(4,0,"日期格式"); sheet.addCell(dates); Label example = new Label(0,1,"資料樣本"); sheet.addCell(example); //浮點數據 Number number = new Number(1,1,3.1415926535); sheet.addCell(number); //整形資料 Number ints = new Number(2,1,15042699); sheet.addCell(ints); Boolean bools = new Boolean(3,1,true); sheet.addCell(bools); //日期型資料 Calendar c = Calendar.getInstance(); Date date = c.getTime(); WritableCellFormat cf1 = new WritableCellFormat(DateFormats.FORMAT1); DateTime dt = new DateTime(4,1,date,cf1); sheet.addCell(dt); //把建立的內容寫入到輸出資料流中,並關閉輸出資料流 } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } workbook.write(); System.out.println("寫入成功"); workbook.close(); */
產生的內容為
2.寫入大資料,在指定的列資料中產生一個隨機數
public String createOrderFile() throws RowsExceededException, WriteException { // 產生隨機單號 randomePlatformID= getRandomNumber(11); randomePlatformID = "PM" + randomePlatformID;
File file = new File(""); //擷取項目的絕對位址
String abspath = file.getAbsolutePath();
createUrl= abspath + "\\OfflineOrders.xls"; Log.logInfo("地址是:" + createUrl); WritableWorkbook workbook = null ; WritableSheet sheet = null; try { workbook = Workbook.createWorkbook(new File(createUrl)); sheet = workbook.createSheet("First Sheet", 0); } catch (IOException e) { e.printStackTrace(); Log.logInfo("建立檔案失敗"); } String[] cellText = { "平台", "所屬人", "對方店鋪帳號", "付款ID", "平台單號", "訂單運費", "發貨倉庫", "發貨渠道", "收件者國家簡碼" , "公司名稱", "收件者姓名","州", "城市", "地址1", "地址2", "郵編", "電話", "Email", "SKU1", "數量1", "單價", "幣種" }; String[] contentText = { "JOOM", "JM3901556", "", "", "PM789", "", "SZ", "DEDHL", "DE", "", "Daniela Weihrauch" ,"Berlin", "Berlin", "Stellingdamm 9", "", "55555", "+49 3068838320", "", "109036701", "3", "17","USD" }; // 產生列頭 for (int i = 0; i < cellText.length; i++) { Label columnHeader = new Label(i, 0, cellText[i]); try { sheet.addCell(columnHeader); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } // 填入本文內容 for (int j = 0; j < contentText.length; j++) { Label columnBody; if (j == 4) {// 平台訂單號單獨處理 Log.logInfo("產生隨機10位元平台訂單編號為:" + randomePlatformID); columnBody = new Label(j, 1, randomePlatformID); } else { columnBody = new Label(j, 1, contentText[j]); } sheet.addCell(columnBody); } try { workbook.write(); Log.logInfo("寫入成功"); try { workbook.close(); } catch (WriteException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return randomePlatformID; }
java產生excel文檔