Java 實現Excel表資料的讀取和寫入 以及過程中可能遇到的問題

來源:互聯網
上載者:User

標籤:ldd   lap   time()   ack   預設   warning   etc   設定字型   cell   

問題1:Unable to recognize OLE stream格式的問題要可能是因為給的資料是2010年的資料表尾碼為.xlsx,要先轉化成2003版的尾碼為.xls問題2:Warning:  Property storage name for 5 is empty - setting to Root Entry可能是jxl.jar  不支援Excel 5.0 for Mac,在Mac下匯入會出現這個問題。//從Excel中讀取資料rowNum行public static double[] readExcel(File excelFile,int rowNum) throws BiffException,IOException{double[] dataX = new double[1000];Workbook rwb = null;Cell cell = null;InputStream stream = new FileInputStream(excelFile);rwb = Workbook.getWorkbook(stream);//擷取指定工作表預設為第一個Sheet sheet = rwb.getSheet(0);//行數,在這裡讀取從第二行開始讀取資料for (int i = 1; i <= rowNum; i++) {String str = new String();//這裡選擇要讀取的資料的列數和行數例如(3,4)說明為第三列第四行,行數都是從0開始計數cell = sheet.getCell(3, i);str = cell.getContents();dataX[i-1] = Double.parseDouble(str);//下面就是為了列印一下資料看一下讀取的資料是否正確System.out.println("Data " + (i-1) +" : " +dataX[i-1]);}return dataX;}//將資料存放區到Excel表中public static void creatExcel(File outFileName,double[] risks) throws BiffException, IOException,WriteException {//獲得輸出資料流OutputStream os = new FileOutputStream(outFileName);WritableWorkbook workbook = Workbook.createWorkbook(os);//在這裡指定你儲存資料表的名稱WritableSheet sheet = workbook.createSheet("Deutsche", 1);Label parameter = new Label(1,1,"1/阿爾法");sheet.addCell(parameter);for (int i = 0; i < risks.length; i++) {Number risk = new Number(1,i+2,risks[i]);sheet.addCell(risk);}//將輸出資料流中資料寫入Excel,關閉輸出資料流workbook.write();workbook.close();os.close();} 一些表結構:::    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);}//複雜布局以及樣式:::public class MutiStyleExcelWrite {    public void createExcel(OutputStream os) throws WriteException,IOException {        //建立工作薄        WritableWorkbook workbook = Workbook.createWorkbook(os);        //建立新的一頁        WritableSheet sheet = workbook.createSheet("First Sheet", 0);        //構造表頭        sheet.mergeCells(0, 0, 4, 0);//添加合併儲存格,第一個參數是起始列,第二個參數是起始行,第三個參數是終止列,第四個參數是終止行        WritableFont bold = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//設定字型種類和黑體顯示,字型為Arial,字型大小大小為10,採用黑體顯示        WritableCellFormat titleFormate = new WritableCellFormat(bold);//產生一個儲存格樣式控制對象        titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//儲存格中的內容水平方向置中        titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//儲存格的內容垂直方向置中        Label title = new Label(0,0,"JExcelApi支援資料類型詳細說明",titleFormate);        sheet.setRowView(0, 600, false);//設定第一行的高度        sheet.addCell(title);                //建立要顯示的具體內容        WritableFont color = new WritableFont(WritableFont.ARIAL);//選擇字型        color.setColour(Colour.GOLD);//設定字型顏色為金黃色        WritableCellFormat colorFormat = new WritableCellFormat(color);        Label formate = new Label(0,1,"資料格式",colorFormat);        sheet.addCell(formate);        Label floats = new Label(1,1,"浮點型");        sheet.addCell(floats);        Label integers = new Label(2,1,"整型");        sheet.addCell(integers);        Label booleans = new Label(3,1,"布爾型");        sheet.addCell(booleans);        Label dates = new Label(4,1,"日期格式");        sheet.addCell(dates);                Label example = new Label(0,2,"資料樣本",colorFormat);        sheet.addCell(example);        //浮點數據        //設定底線        WritableFont underline= new WritableFont(WritableFont.ARIAL,WritableFont.DEFAULT_POINT_SIZE,WritableFont.NO_BOLD,false,UnderlineStyle.SINGLE);        WritableCellFormat greyBackground = new WritableCellFormat(underline);        greyBackground.setBackground(Colour.GRAY_25);//設定背景顏色為灰色        Number number = new Number(1,2,3.1415926535,greyBackground);        sheet.addCell(number);        //整形資料        WritableFont boldNumber = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑體        WritableCellFormat boldNumberFormate = new WritableCellFormat(boldNumber);        Number ints = new Number(2,2,15042699,boldNumberFormate);        sheet.addCell(ints);        //布爾型資料        Boolean bools = new Boolean(3,2,true);        sheet.addCell(bools);        //日期型資料        //設定黑體和底線        WritableFont boldDate = new WritableFont(WritableFont.ARIAL,WritableFont.DEFAULT_POINT_SIZE,WritableFont.BOLD,false,UnderlineStyle.SINGLE);        WritableCellFormat boldDateFormate = new WritableCellFormat(boldDate,DateFormats.FORMAT1);        Calendar c = Calendar.getInstance();        Date date = c.getTime();        DateTime dt = new DateTime(4,2,date,boldDateFormate);        sheet.addCell(dt);        //把建立的內容寫入到輸出資料流中,並關閉輸出資料流        workbook.write();        workbook.close();        os.close();            }

 

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.