java產生excel文檔

來源:互聯網
上載者:User

標籤:電話   下載   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文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.