溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java簡單解析xls文件的方法示例【讀取和寫入】

發(fā)布時間:2020-08-26 17:09:52 來源:腳本之家 閱讀:120 作者:qq7342272 欄目:編程語言

本文實例講述了java簡單解析xls文件的方法。分享給大家供大家參考,具體如下:

讀?。?/strong>

import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;
class Aa{
public static void main(String args[]) {
try{
Workbook workbook = null;
try {
  workbook = Workbook.getWorkbook(new File("d:\\a.xls"));
} catch (Exception e) {
  throw new Exception("file to import not found!");
}
Sheet sheet = workbook.getSheet(0);
Cell cell = null;
int columnCount=3;
int rowCount=sheet.getRows();
for (int i = 0; i <rowCount; i++) {
  for (int j = 0; j <columnCount; j++) {
    //注意,這里的兩個參數(shù),第一個是表示列的,第二才表示行
    cell=sheet.getCell(j, i);
    //要根據(jù)單元格的類型分別做處理,否則格式化過的內(nèi)容可能會不正確
    if(cell.getType()==CellType.NUMBER){
      System.out.print(((NumberCell)cell).getValue());
    }
    else if(cell.getType()==CellType.DATE){
      System.out.print(((DateCell)cell).getDate());
    }
    else{
      System.out.print(cell.getContents());
    }
    //System.out.print(cell.getContents());
    System.out.print("\t");
  }
  System.out.print("\n");
}
//關(guān)閉它,否則會有內(nèi)存泄露
workbook.close();
}catch(Exception e){
}
}
}

寫入:

import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;
class Aa{
public static void main(String args[]) {
try{
File tempFile=new File("d:" + java.io.File.separator + "output00.xls");
System.out.println( "d:" + java.io.File.separator + "output00.xls" );
WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
//一些臨時變量,用于寫到excel中
Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;
//預(yù)定義的一些字體和格式,同一個Excel中最好不要有太多格式
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat (headerFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat (titleFont);
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat (detFont);
NumberFormat nf=new NumberFormat("0.00000"); //用于Number的格式
WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);
DateFormat df=new DateFormat("yyyy-MM-dd");//用于日期的
WritableCellFormat dateFormat = new WritableCellFormat (detFont, df);
//剩下的事情,就是用上面的內(nèi)容和格式創(chuàng)建一些單元格,再加到sheet中
l=new Label(0, 0, "用于測試的Excel文件", headerFormat);
sheet.addCell(l);
//add Title
int column=0;
l=new Label(column++, 2, "標(biāo)題", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "日期", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "貨幣", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "價格", titleFormat);
sheet.addCell(l);
//add detail
int i=0;
column=0;
l=new Label(column++, i+3, "標(biāo)題 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "CNY", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
sheet.addCell(n);
i++;
column=0;
l=new Label(column++, i+3, "標(biāo)題 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "SGD", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
sheet.addCell(n);
//設(shè)置列的寬度
column=0;
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 10);
sheet.setColumnView(column++, 20);
workbook.write();
workbook.close();
}catch(Exception e){
}
}
}

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設(shè)計有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI