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

java實現(xiàn)Excel的導(dǎo)入、導(dǎo)出

 更新時間:2020年06月09日 08:41:17   作者:cczheng  
這篇文章主要為大家詳細介紹了java實現(xiàn)Excel的導(dǎo)入、導(dǎo)出的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、Excel的導(dǎo)入

導(dǎo)入可采用兩種方式,一種是JXL,另一種是POI,但前者不能讀取高版本的Excel(07以上),后者更具兼容性。由于對兩種方式都進行了嘗試,就都貼出來分享(若有錯誤,請給予指正)

方式一、JXL導(dǎo)入  所需jar包 JXL.jar

publicstaticList<PutStorageInfo> readExcelByJXL(String filePath){
List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>();
Map<String,List<String>> map =newHashMap<String,List<String>>();
 infoList.clear();
try{
InputStream is =newFileInputStream(filePath);
Workbook workbook =Workbook.getWorkbook(is);
//獲取第1張表
Sheet sheet = workbook.getSheet(0);
//獲取總的列數(shù)
int columns = sheet.getColumns();
//獲取總的行數(shù)
int rows = sheet.getRows();
//先列后行(j,i)
for(int i =1; i < rows; i++){
List<String> contentList =newArrayList<String>();
 contentList.clear();
for(int j =1; j < columns; j++){
 contentList.add(sheet.getCell(j,i).getContents());
}
 map.put("StorageInfo"+i, contentList);
}

//遍歷map集合,封裝成bean
for(Map.Entry<String,List<String>> entry : map.entrySet()){
List<String> list = entry.getValue();
PutStorageInfo storageInfo =newPutStorageInfo();
 storageInfo.setProductcode(list.get(0));
 storageInfo.setProductsort(list.get(1));
 storageInfo.setProductbrand(list.get(2));
 storageInfo.setProductname(list.get(3));
 storageInfo.setProductquantity(list.get(4));
 storageInfo.setProductcontent(list.get(5));
 storageInfo.setProductnetweight(list.get(6));
 storageInfo.setProductcountry(list.get(7));
 storageInfo.setProductpdate(list.get(8));
 storageInfo.setProductprice(list.get(9));
 storageInfo.setProductmark(list.get(10));

 infoList.add(storageInfo);
}
 is.close();
}catch(Exception e){
 e.printStackTrace();
}
return infoList;
}

方式二、POI導(dǎo)入 

所需jar包
poi-3.6-20091214.jar
poi-ooxml-3.6-20091214.jar
poi-ooxml-schemas-3.6-20091214.jar
xmlbeans-2.3.0.jar
dom4j-1.6.1.jar
jdom-2.0.6.jar

publicstaticList<PutStorageInfo> readExcelByPOI(String filePath){
List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>();
Map<String,List<String>> map =newHashMap<String,List<String>>();
 infoList.clear();
try{
InputStream is =newFileInputStream(filePath);

int index = filePath.lastIndexOf(".");
String postfix = filePath.substring(index+1);

Workbook workbook =null;
if("xls".equals(postfix)){
 workbook =newHSSFWorkbook(is);
}elseif("xlsx".equals(postfix)){
 workbook =newXSSFWorkbook(is);
}
//獲取第1張表
Sheet sheet = workbook.getSheetAt(0);
//總的行數(shù)
int rows = sheet.getLastRowNum();
//總的列數(shù)--->最后一列為null則有問題,讀取不完整,將表頭的數(shù)目作為總的列數(shù),沒有的則補為null
int columns = sheet.getRow(0).getLastCellNum();
//先列后行
for(int i =1; i <= rows; i++){
  Row row = sheet.getRow(i);
 if(null!= row && row.getFirstCellNum()==-1){//這一行是空行,不讀取
 continue;
}
//這一行的總列數(shù)
// columns = row.getLastCellNum();
List<String> contentList =newArrayList<String>();
 contentList.clear();
for(int j =1; j < columns; j++){
if(row.getCell(j)!=null){
 row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
 contentList.add(row.getCell(j).getStringCellValue());
}else{
 contentList.add("");
}
}
 map.put("StorageInfo"+i, contentList);
}

//遍歷map集合,封裝成bean
for(Map.Entry<String,List<String>> entry : map.entrySet()){
List<String> list = entry.getValue();
PutStorageInfo storageInfo =newPutStorageInfo();
 storageInfo.setProductcode(list.get(0));
 storageInfo.setProductsort(list.get(1));
 storageInfo.setProductbrand(list.get(2));
 storageInfo.setProductname(list.get(3));
 storageInfo.setProductquantity(list.get(4));
 storageInfo.setProductcontent(list.get(5));
 storageInfo.setProductnetweight(list.get(6));
 storageInfo.setProductcountry(list.get(7));
 storageInfo.setProductpdate(list.get(8));
 storageInfo.setProductprice(list.get(9));
 storageInfo.setProductmark(list.get(10));

 infoList.add(storageInfo);
}
 is.close();
}catch(Exception e){
 e.printStackTrace();
}

return infoList;
} 

二、Excel導(dǎo)出

采用JXL實現(xiàn)

publicstaticvoid creatExcel(List<PutStorageInfo> storageInfoList,String fileName){
try{
OutputStream os =newFileOutputStream(fileName);
//創(chuàng)建可寫的工作薄
WritableWorkbook workbook =Workbook.createWorkbook(os);
//創(chuàng)建第一張表
WritableSheet sheet = workbook.createSheet("Sheet1",0);
//設(shè)置根據(jù)內(nèi)容自動寬度
CellView cellView =newCellView();
 cellView.setAutosize(true);
//在下邊f(xié)or循環(huán)中為每一列設(shè)置

//設(shè)置列寬度,此種方式參數(shù)的意思,i-->對應(yīng)的行或列 j-->要設(shè)置的寬度
// sheet.setColumnView(0, 100);
// sheet.setRowView(0, 300);
//設(shè)置字體加粗且背景顏色為黃色
WritableFont boldFont =newWritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑體 
WritableCellFormat cellrFormate =newWritableCellFormat(boldFont);
 cellrFormate.setBackground(Colour.YELLOW);
//先添加表頭
List<String> titleList = getTitleList();
//循環(huán)創(chuàng)建單元格,先列后行
for(int i =0; i < titleList.size(); i++){
//sheet.setColumnView(i, cellView);
 sheet.setColumnView(i,20);

Label label =newLabel(i,0, titleList.get(i), cellrFormate);
 sheet.addCell(label);
}

LogUtil.logOut(JXLWriteExcel.class,storageInfoList.size()+"");

String[][] content = convertToArr(storageInfoList);
//設(shè)置content的自適應(yīng)當(dāng)前列的寬度,文本太對會自動換行 new Label(j, i+1, content[i][j-1],contentFormat);
WritableCellFormat contentFormat =newWritableCellFormat();
 contentFormat.setWrap(true);

//然后添加入庫信息條目
for(int i =0; i < storageInfoList.size(); i++){
Label labelID =newLabel(0,i+1,(i+1)+"");
 sheet.addCell(labelID);

for(int j =1; j < titleList.size(); j++){
Label label =newLabel(j, i+1, content[i][j-1]);
 sheet.addCell(label);
}
}

//把創(chuàng)建的內(nèi)容寫入到輸出流中,并關(guān)閉輸出流
workbook.write();
 workbook.close();
 os.close();

//將存儲了入庫bean的list清空
storageInfoList.clear();

}catch(Exception e){
 e.printStackTrace();
}
}

privatestaticString[][] convertToArr(List<PutStorageInfo> storageInfoList){
String[][] content =newString[storageInfoList.size()][11];
for(int i =0; i < storageInfoList.size(); i++){
PutStorageInfo info = storageInfoList.get(i);
//每個bean中總項有11項
content[i][0]= info.getProductcode();
 content[i][1]= info.getProductsort();
 content[i][2]= info.getProductbrand();
 content[i][3]= info.getProductname();
 content[i][4]= info.getProductquantity();
 content[i][5]= info.getProductcontent();
 content[i][6]= info.getProductnetweight();
 content[i][7]= info.getProductcountry();
 content[i][8]= info.getProductpdate();
 content[i][9]= info.getProductprice();
 content[i][10]= info.getProductmark();
}
return content;

}

privatestaticList<String> getTitleList(){
List<String> list =newArrayList<String>();
 list.add("Item No.");
 list.add("Product code");
 list.add("Sort");
 list.add("Brand");
 list.add("Product Name");
 list.add("Quantity(Pieces)");
 list.add("Content");
list.add("Net Weight");
 list.add("Country");
 list.add("Best before date");
 list.add("Price(EURO)");
 list.add("Remarks");

return list;
}

 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn)

    spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn)

    這篇文章主要介紹了spring @Validated 注解開發(fā)中使用group分組校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • arthas排查jvm中CPU占用過高問題解決

    arthas排查jvm中CPU占用過高問題解決

    這篇文章主要介紹了arthas排查jvm中CPU占用過高問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • 詳解Java中AC自動機的原理與實現(xiàn)

    詳解Java中AC自動機的原理與實現(xiàn)

    AC自動機是一個多模式匹配算法,在模式匹配領(lǐng)域被廣泛應(yīng)用。本文將詳細為大家介紹AC自動機的原理與實現(xiàn)方法,感興趣的可以了解一下
    2022-05-05
  • 基于NIO的Netty網(wǎng)絡(luò)框架(詳解)

    基于NIO的Netty網(wǎng)絡(luò)框架(詳解)

    下面小編就為大家?guī)硪黄贜IO的Netty網(wǎng)絡(luò)框架(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • SpringBoot中使用異步調(diào)度程序的高級方法

    SpringBoot中使用異步調(diào)度程序的高級方法

    本文主要介紹了SpringBoot中使用異步調(diào)度程序的高級方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • MybatisPlus代碼生成器使用示例

    MybatisPlus代碼生成器使用示例

    MyBatis-Plus自動化的生成與數(shù)據(jù)庫表對應(yīng)的Java代碼文件,本文主要介紹了MybatisPlus代碼生成器使用示例,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • 詳解Spring ApplicationContext加載過程

    詳解Spring ApplicationContext加載過程

    這篇文章主要介紹了Spring ApplicationContext加載過程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下
    2021-03-03
  • SpringBoot如何自定義線程池配置類

    SpringBoot如何自定義線程池配置類

    有時候我們在項目中做一些長鏈路的跑批任務(wù)時,基于Springboot項目的定時任務(wù),我們可以指定一個自定義的線程配置類進行單獨提供給具體跑批任務(wù)使用,而不占用整個系統(tǒng)資源,這篇文章主要介紹了SpringBoot如何自定義線程池配置類,需要的朋友可以參考下
    2024-04-04
  • Spring中使用騰訊云發(fā)送短信驗證碼的實現(xiàn)示例

    Spring中使用騰訊云發(fā)送短信驗證碼的實現(xiàn)示例

    本文主要介紹了Spring?中?使用騰訊云發(fā)送短信驗證碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Java多線程實現(xiàn)阻塞隊列的示例代碼

    Java多線程實現(xiàn)阻塞隊列的示例代碼

    本文主要介紹了Java多線程實現(xiàn)阻塞隊列的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12

最新評論

宁蒗| 大邑县| 商城县| 漳浦县| 望都县| 亚东县| 淮安市| 麦盖提县| 永春县| 安庆市| 隆化县| 新干县| 甘孜| 四川省| 花莲市| 新邵县| 凤冈县| 徐州市| 洛阳市| 百色市| 龙胜| 昌都县| 城步| 武川县| 偏关县| 大丰市| 同江市| 中西区| 西畴县| 大荔县| 永川市| 滕州市| 隆安县| 抚州市| 静安区| 仲巴县| 阿拉善左旗| 合川市| 丹东市| 远安县| 江安县|