最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年06月02日 10:55:07   作者:qq7342272  
這篇文章主要介紹了java簡單解析xls文件的方法,結(jié)合實(shí)例形式分析了java針對xls文件的讀取和寫入相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下

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

讀取:

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é)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

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

相關(guān)文章

  • SpringBoot使用OpenCV示例總結(jié)

    SpringBoot使用OpenCV示例總結(jié)

    這篇文章主要介紹了SpringBoot使用OpenCV示例總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 前端dist包放到后端springboot項(xiàng)目下一起打包圖文教程

    前端dist包放到后端springboot項(xiàng)目下一起打包圖文教程

    這篇文章主要介紹了前端dist包放到后端springboot項(xiàng)目下一起打包的相關(guān)資料,具體步驟包括前端打包、將前端文件復(fù)制到后端項(xiàng)目的static目錄、后端打包、驗(yàn)證部署成功等,需要的朋友可以參考下
    2025-01-01
  • 基于SSM+Shiro+Bootstrap實(shí)現(xiàn)用戶權(quán)限管理系統(tǒng)

    基于SSM+Shiro+Bootstrap實(shí)現(xiàn)用戶權(quán)限管理系統(tǒng)

    這篇文章主要介紹了基于SSM+Shiro實(shí)現(xiàn)一個用戶權(quán)限管理系統(tǒng),每位用戶只可訪問指定的頁面,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,快跟隨小編一起學(xué)習(xí)吧
    2021-12-12
  • 詳解mybatis @SelectProvider 注解

    詳解mybatis @SelectProvider 注解

    這篇文章主要介紹了mybatis @SelectProvider 注解的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-12-12
  • 基于SpringMVC接受JSON參數(shù)詳解及常見錯誤總結(jié)

    基于SpringMVC接受JSON參數(shù)詳解及常見錯誤總結(jié)

    下面小編就為大家分享一篇基于SpringMVC接受JSON參數(shù)詳解及常見錯誤總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼校驗(yàn)方法思路詳解

    SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼校驗(yàn)方法思路詳解

    最近做項(xiàng)目遇到這樣的需求,前端是基于BootStrap,html代碼中有BootStrap樣式實(shí)現(xiàn)的,具體后臺實(shí)現(xiàn)代碼大家通過本文一起學(xué)習(xí)吧
    2017-08-08
  • selenium+java破解極驗(yàn)滑動驗(yàn)證碼的示例代碼

    selenium+java破解極驗(yàn)滑動驗(yàn)證碼的示例代碼

    本篇文章主要介紹了selenium+java破解極驗(yàn)滑動驗(yàn)證碼的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Idea跑的項(xiàng)目沒問題將程序install成jar包運(yùn)行報錯空指針的問題

    Idea跑的項(xiàng)目沒問題將程序install成jar包運(yùn)行報錯空指針的問題

    這篇文章主要介紹了Idea跑的項(xiàng)目沒問題,將程序install成jar包運(yùn)行報錯空指針的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Spring Security入門demo案例

    Spring Security入門demo案例

    Spring Security是一個高度自定義的安全框架,本文主要介紹了Spring Security入門,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java 實(shí)例解析單例模式

    Java 實(shí)例解析單例模式

    單例模式(Singleton Pattern)是 Java 中最簡單的設(shè)計(jì)模式之一。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式,這種模式涉及到一個單一的類,該類負(fù)責(zé)創(chuàng)建自己的對象,同時確保只有單個對象被創(chuàng)建
    2021-11-11

最新評論

于都县| 东乌珠穆沁旗| 陆河县| 孟村| 西青区| 林芝县| 内乡县| 吉隆县| 大兴区| 无锡市| 黄龙县| 济源市| 安国市| 南部县| 双鸭山市| 阿勒泰市| 邓州市| 进贤县| 织金县| 定州市| 宜城市| 和田市| 拜城县| 名山县| 临武县| 特克斯县| 呼图壁县| 台湾省| 景东| 吴旗县| 阳朔县| 富蕴县| 榕江县| 娄底市| 安远县| 林甸县| 察隅县| 靖安县| 泗水县| 太康县| 建瓯市|