java簡(jiǎn)單解析xls文件的方法示例【讀取和寫(xiě)入】
本文實(shí)例講述了java簡(jiǎn)單解析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++) {
//注意,這里的兩個(gè)參數(shù),第一個(gè)是表示列的,第二才表示行
cell=sheet.getCell(j, i);
//要根據(jù)單元格的類(lèi)型分別做處理,否則格式化過(guò)的內(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();
}catch(Exception e){
}
}
}
寫(xiě)入:
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);
//一些臨時(shí)變量,用于寫(xiě)到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();
}catch(Exception e){
}
}
}
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- java_IO向文件中寫(xiě)入和讀取內(nèi)容代碼實(shí)例
- Java 按行讀取文件按行寫(xiě)入文件并以空格分割字符串的方法
- Java中IO流文件讀取、寫(xiě)入和復(fù)制的實(shí)例
- Java RandomAccessFile 指定位置實(shí)現(xiàn)文件讀取與寫(xiě)入
- Java讀取txt文件和寫(xiě)入txt文件的簡(jiǎn)單實(shí)例
- Java讀取、寫(xiě)入文件如何解決亂碼問(wèn)題
- java按指定編碼寫(xiě)入和讀取文件內(nèi)容的類(lèi)分享
- Java實(shí)現(xiàn)文件讀取和寫(xiě)入過(guò)程解析
相關(guān)文章
一個(gè)MIDP俄羅斯方塊游戲的設(shè)計(jì)和實(shí)現(xiàn)
一個(gè)MIDP俄羅斯方塊游戲的設(shè)計(jì)和實(shí)現(xiàn)...2006-12-12
詳解在Spring-Boot中實(shí)現(xiàn)通用Auth認(rèn)證的幾種方式
這篇文章主要介紹了詳解在Spring-Boot中實(shí)現(xiàn)通用Auth認(rèn)證的幾種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Eclipse中使用Maven創(chuàng)建Java Web工程的實(shí)現(xiàn)方式
這篇文章主要介紹了Eclipse中使用Maven創(chuàng)建Java Web工程的實(shí)現(xiàn)方式的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的方式,需要的朋友可以參考下2017-10-10
java中的數(shù)學(xué)計(jì)算函數(shù)的總結(jié)
這篇文章主要介紹了java中的數(shù)學(xué)計(jì)算函數(shù)的總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07
java實(shí)現(xiàn)識(shí)別二維碼圖片功能方法詳解與實(shí)例源碼
這篇文章主要介紹了java實(shí)現(xiàn)識(shí)別二維碼圖片,java無(wú)法識(shí)別二維碼情況下對(duì)二維碼圖片調(diào)優(yōu)功能方法與實(shí)例源碼,需要的朋友可以參考下2022-12-12
spring cloud gateway網(wǎng)關(guān)路由分配代碼實(shí)例解析
這篇文章主要介紹了spring cloud gateway網(wǎng)關(guān)路由分配代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Mybatis反射核心類(lèi)Reflector的實(shí)現(xiàn)
本文主要介紹了Mybatis反射核心類(lèi)Reflector的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

