原作者:SonyMusic
讀:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
在Java中讀取Excel文件的內(nèi)容
在這里,我使用的是一個(gè)叫Java Excel API的東西,類似的還有jakarta的POI,不過感覺那個(gè)
太復(fù)雜了點(diǎn)兒。而且jxl對(duì)中文的支持相當(dāng)?shù)暮茫辽傥以谟玫倪^程中一點(diǎn)問題沒出。
一、下載地址
http://sourceforge.net/project/showfiles.php?group_id=79926二、特性
可以讀取Excel 95, 97, 2000文件
可以讀或?qū)慐xcel 97及其以后版本的的公式(不過我發(fā)現(xiàn)好像有bug)
生成Excel 97格式的電子表格
支持字體、數(shù)字和日期格式化
支持單元格的顏色和陰影
可以編輯現(xiàn)有的文件
三、讀文件
//聲明一下,記得后面要關(guān)閉哦。。
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File("d:\\temp\\TestRead.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++) {
//注意,這里的兩個(gè)參數(shù),第一個(gè)是表示列的,第二才表示行
cell=sheet.getCell(j, i);
//要根據(jù)單元格的類型分別做處理,否則格式化過的內(nèi)容可能會(huì)不正確
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)閉它,否則會(huì)有內(nèi)存泄露
workbook.close();
寫:wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
在Java中向Excel文件寫入內(nèi)容
四、導(dǎo)出數(shù)據(jù)到Excel文件中
下面的例子,設(shè)置了數(shù)字、日期的格式,還有字體,顏色等。
File tempFile=new File("d:/temp/output.xls");
WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
//一些臨時(shí)變量,用于寫到excel中
Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;
//預(yù)定義的一些字體和格式,同一個(gè)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, "用于測(cè)試的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, "價(jià)格", 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();