日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

poi java 导入excel_Java的poi技术读取和导入Excel

發(fā)布時間:2023/12/20 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poi java 导入excel_Java的poi技术读取和导入Excel 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

項目結構:

用到的Excel文件:

XlsMain .java 類

//該類有main方法,主要負責運行程序,同時該類中也包含了用poi讀取Excel(2003版)

import?java.io.FileInputStream;

import?java.io.IOException;

import?java.io.InputStream;

import?java.util.ArrayList;

import?java.util.List;

import?org.apache.poi.hssf.usermodel.HSSFCell;

import?org.apache.poi.hssf.usermodel.HSSFRow;

import?org.apache.poi.hssf.usermodel.HSSFSheet;

import?org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**

*

*?@author?Hongten

*

*?????????參考地址:http://hao0610.iteye.com/blog/1160678

*

*/

public?class?XlsMain?{

public?static?void?main(String[]?args)?throws?IOException?{

XlsMain?xlsMain?=?new?XlsMain();

XlsDto?xls?=?null;

List?list?=?xlsMain.readXls();

try?{

XlsDto2Excel.xlsDto2Excel(list);

}?catch?(Exception?e)?{

e.printStackTrace();

}

for?(int?i?=?0;?i?

xls?=?(XlsDto)?list.get(i);

System.out.println(xls.getXh()?+?"????"?+?xls.getXm()?+?"????"

+?xls.getYxsmc()?+?"????"?+?xls.getKcm()?+?"????"

+?xls.getCj());

}

}

/**

*?讀取xls文件內容

*

*?@return?List對象

*?@throws?IOException

*?????????????輸入/輸出(i/o)異常

*/

private?List?readXls()?throws?IOException?{

InputStream?is?=?new?FileInputStream("pldrxkxxmb.xls");

HSSFWorkbook?hssfWorkbook?=?new?HSSFWorkbook(is);

XlsDto?xlsDto?=?null;

List?list?=?new?ArrayList();

//?循環(huán)工作表Sheet

for?(int?numSheet?=?0;?numSheet?

HSSFSheet?hssfSheet?=?hssfWorkbook.getSheetAt(numSheet);

if?(hssfSheet?==?null)?{

continue;

}

//?循環(huán)行Row

for?(int?rowNum?=?1;?rowNum?<=?hssfSheet.getLastRowNum();?rowNum++)?{

HSSFRow?hssfRow?=?hssfSheet.getRow(rowNum);

if?(hssfRow?==?null)?{

continue;

}

xlsDto?=?new?XlsDto();

//?循環(huán)列Cell

//?0學號?1姓名?2學院?3課程名?4?成績

//?for?(int?cellNum?=?0;?cellNum?<=4;?cellNum++)?{

HSSFCell?xh?=?hssfRow.getCell(0);

if?(xh?==?null)?{

continue;

}

xlsDto.setXh(getValue(xh));

HSSFCell?xm?=?hssfRow.getCell(1);

if?(xm?==?null)?{

continue;

}

xlsDto.setXm(getValue(xm));

HSSFCell?yxsmc?=?hssfRow.getCell(2);

if?(yxsmc?==?null)?{

continue;

}

xlsDto.setYxsmc(getValue(yxsmc));

HSSFCell?kcm?=?hssfRow.getCell(3);

if?(kcm?==?null)?{

continue;

}

xlsDto.setKcm(getValue(kcm));

HSSFCell?cj?=?hssfRow.getCell(4);

if?(cj?==?null)?{

continue;

}

xlsDto.setCj(Float.parseFloat(getValue(cj)));

list.add(xlsDto);

}

}

return?list;

}

/**

*?得到Excel表中的值

*

*?@param?hssfCell

*????????????Excel中的每一個格子

*?@return?Excel中每一個格子中的值

*/

@SuppressWarnings("static-access")

private?String?getValue(HSSFCell?hssfCell)?{

if?(hssfCell.getCellType()?==?hssfCell.CELL_TYPE_BOOLEAN)?{

//?返回布爾類型的值

return?String.valueOf(hssfCell.getBooleanCellValue());

}?else?if?(hssfCell.getCellType()?==?hssfCell.CELL_TYPE_NUMERIC)?{

//?返回數(shù)值類型的值

return?String.valueOf(hssfCell.getNumericCellValue());

}?else?{

//?返回字符串類型的值

return?String.valueOf(hssfCell.getStringCellValue());

}

}

}

XlsDto2Excel.java類

//該類主要負責向Excel(2003版)中插入數(shù)據(jù)

import?java.io.FileOutputStream;

import?java.io.OutputStream;

import?java.util.List;

import?org.apache.poi.hssf.usermodel.HSSFCell;

import?org.apache.poi.hssf.usermodel.HSSFRichTextString;

import?org.apache.poi.hssf.usermodel.HSSFRow;

import?org.apache.poi.hssf.usermodel.HSSFSheet;

import?org.apache.poi.hssf.usermodel.HSSFWorkbook;

public?class?XlsDto2Excel?{

/**

*

*?@param?xls

*????????????XlsDto實體類的一個對象

*?@throws?Exception

*?????????????在導入Excel的過程中拋出異常

*/

public?static?void?xlsDto2Excel(List?xls)?throws?Exception?{

//?獲取總列數(shù)

int?CountColumnNum?=?xls.size();

//?創(chuàng)建Excel文檔

HSSFWorkbook?hwb?=?new?HSSFWorkbook();

XlsDto?xlsDto?=?null;

//?sheet?對應一個工作頁

HSSFSheet?sheet?=?hwb.createSheet("pldrxkxxmb");

HSSFRow?firstrow?=?sheet.createRow(0);?//?下標為0的行開始

HSSFCell[]?firstcell?=?new?HSSFCell[CountColumnNum];

String[]?names?=?new?String[CountColumnNum];

names[0]?=?"學號";

names[1]?=?"姓名";

names[2]?=?"學院";

names[3]?=?"課程名";

names[4]?=?"成績";

for?(int?j?=?0;?j?

firstcell[j]?=?firstrow.createCell(j);

firstcell[j].setCellValue(new?HSSFRichTextString(names[j]));

}

for?(int?i?=?0;?i?

//?創(chuàng)建一行

HSSFRow?row?=?sheet.createRow(i?+?1);

//?得到要插入的每一條記錄

xlsDto?=?xls.get(i);

for?(int?colu?=?0;?colu?<=?4;?colu++)?{

//?在一行內循環(huán)

HSSFCell?xh?=?row.createCell(0);

xh.setCellValue(xlsDto.getXh());

HSSFCell?xm?=?row.createCell(1);

xm.setCellValue(xlsDto.getXm());

HSSFCell?yxsmc?=?row.createCell(2);

yxsmc.setCellValue(xlsDto.getYxsmc());

HSSFCell?kcm?=?row.createCell(3);

kcm.setCellValue(xlsDto.getKcm());

HSSFCell?cj?=?row.createCell(4);

cj.setCellValue(xlsDto.getCj());

(xlsDto.getMessage());

}

}

//?創(chuàng)建文件輸出流,準備輸出電子表格

OutputStream?out?=?new?FileOutputStream("POI2Excel/pldrxkxxmb.xls");

hwb.write(out);

out.close();

System.out.println("數(shù)據(jù)庫導出成功");

}

}

XlsDto .java類

//該類是一個實體類

public?class?XlsDto?{

/**

*?選課號

*/

private?Integer?xkh;

/**

*?學號

*/

private?String?xh;

/**

*?姓名

*/

private?String?xm;

/**

*?學院

*/

private?String?yxsmc;

/**

*?課程號

*/

private?Integer?kch;

/**

*?課程名

*/

private?String?kcm;

/**

*?成績

*/

private?float?cj;

public?Integer?getXkh()?{

return?xkh;

}

public?void?setXkh(Integer?xkh)?{

this.xkh?=?xkh;

}

public?String?getXh()?{

return?xh;

}

public?void?setXh(String?xh)?{

this.xh?=?xh;

}

public?String?getXm()?{

return?xm;

}

public?void?setXm(String?xm)?{

this.xm?=?xm;

}

public?String?getYxsmc()?{

return?yxsmc;

}

public?void?setYxsmc(String?yxsmc)?{

this.yxsmc?=?yxsmc;

}

public?Integer?getKch()?{

return?kch;

}

public?void?setKch(Integer?kch)?{

this.kch?=?kch;

}

public?String?getKcm()?{

return?kcm;

}

public?void?setKcm(String?kcm)?{

this.kcm?=?kcm;

}

public?float?getCj()?{

return?cj;

}

public?void?setCj(float?cj)?{

this.cj?=?cj;

}

}

后臺輸出:

數(shù)據(jù)庫導出成功

1.0 hongten 信息技術學院 計算機網絡應用基礎 80.0

2.0 王五 信息技術學院 計算機網絡應用基礎 81.0

3.0 李勝基 信息技術學院 計算機網絡應用基礎 82.0

4.0 五班古 信息技術學院 計算機網絡應用基礎 83.0

5.0 蔡詩蕓 信息技術學院 計算機網絡應用基礎 84.0

總結

以上是生活随笔為你收集整理的poi java 导入excel_Java的poi技术读取和导入Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。