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


問題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(); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM