導航:首頁 > 文件類型 > POI讀取Excel文件類型

POI讀取Excel文件類型

發布時間:2022-08-19 16:54:57

1. 請教POI解析xlsx時報錯

POI讀取Excel有兩種模式,一種是用戶模式,一種是SAX模式,將xlsx格式的文檔轉換成CVS格式後再進行處理用戶模式相信大家都很清楚,也是POI常用的方式,用戶模式API介面豐富,我們可以很容易的使用POI的API讀取Excel,但用戶模式消耗的內存很大,當遇到很多sheet、大數據網格、假空行、公式等問題時,很容易導致內存溢出。POI官方推薦解決內存溢出的方式使用CVS格式解析,我們不可能手工將Excel文件轉換成CVS格式再上傳,這樣做太麻煩了,好再POI給出了xlsx轉換CVS的例子,基於這個例子我進行了一下改造,即可解決用戶模式讀取Excel內存溢出的問題。下面附上代碼

2. 要用java POI讀取Excel文件中的數據,並且實現對數據的格式校驗,輸入錯誤信息

太籠統了,只能給出一個POI讀取Excel的大致方法。
對數據的校驗,與具體的文件有關。
你定義的那一列是時間類型的,那一列是數字列的。
盲目的讀取,無法判斷數據的類型。

3. java poi怎麼獲取excel單元格的內容

js">packagee.sjtu.erplab.poi;

importjava.io.InputStream&ch=ww.xqy.chain"target="_blank"class="link-ke">FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStream;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;

importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFDateUtil;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
*操作Excel表格的功能類
*/
publicclassExcelReader{
privatePOIFSFileSystemfs;
privateHSSFWorkbookwb;
privateHSSFSheetsheet;
privateHSSFRowrow;

/**
*讀取Excel表格表頭的內容
*@paramInputStream
*@returnString表頭內容的數組
*/
publicString[]readExcelTitle(InputStreamis){
try{
fs=newPOIFSFileSystem(is);
wb=newHSSFWorkbook(fs);
}catch(IOExceptione){
e.printStackTrace();
}
sheet=wb.getSheetAt(0);
row=sheet.getRow(0);
//標題總列數
intcolNum=row.getPhysicalNumberOfCells();
System.out.println("colNum:"+colNum);
String[]title=newString[colNum];
for(inti=0;i<colNum;i++){
//title[i]=getStringCellValue(row.getCell((short)i));
title[i]=getCellFormatValue(row.getCell((short)i));
}
returntitle;
}

/**
*讀取Excel數據內容
*@paramInputStream
*@returnMap包含單元格數據內容的Map對象
*/
publicMap<Integer,String>readExcelContent(InputStreamis){
Map<Integer,String>content=newHashMap<Integer,String>();
Stringstr="";
try{
fs=newPOIFSFileSystem(is);
wb=newHSSFWorkbook(fs);
}catch(IOExceptione){
e.printStackTrace();
}
sheet=wb.getSheetAt(0);
//得到總行數
introwNum=sheet.getLastRowNum();
row=sheet.getRow(0);
intcolNum=row.getPhysicalNumberOfCells();
//正文內容應該從第二行開始,第一行為表頭的標題
for(inti=1;i<=rowNum;i++){
row=sheet.getRow(i);
intj=0;
while(j<colNum){
//每個單元格的數據內容用"-"分割開,以後需要時用String類的replace()方法還原數據
//也可以將每個單元格的數據設置到一個javabean的屬性中,此時需要新建一個javabean
//str+=getStringCellValue(row.getCell((short)j)).trim()+
//"-";
str+=getCellFormatValue(row.getCell((short)j)).trim()+"";
j++;
}
content.put(i,str);
str="";
}
returncontent;
}

/**
*獲取單元格數據內容為字元串類型的數據
*
*@paramcellExcel單元格
*@returnString單元格數據內容
*/
(HSSFCellcell){
StringstrCell="";
switch(cell.getCellType()){
caseHSSFCell.CELL_TYPE_STRING:
strCell=cell.getStringCellValue();
break;
caseHSSFCell.CELL_TYPE_NUMERIC:
strCell=String.valueOf(cell.getNumericCellValue());
break;
caseHSSFCell.CELL_TYPE_BOOLEAN:
strCell=String.valueOf(cell.getBooleanCellValue());
break;
caseHSSFCell.CELL_TYPE_BLANK:
strCell="";
break;
default:
strCell="";
break;
}
if(strCell.equals("")||strCell==null){
return"";
}
if(cell==null){
return"";
}
returnstrCell;
}

/**
*獲取單元格數據內容為日期類型的數據
*
*@paramcell
*Excel單元格
*@returnString單元格數據內容
*/
privateStringgetDateCellValue(HSSFCellcell){
Stringresult="";
try{
intcellType=cell.getCellType();
if(cellType==HSSFCell.CELL_TYPE_NUMERIC){
Datedate=cell.getDateCellValue();
result=(date.getYear()+1900)+"-"+(date.getMonth()+1)
+"-"+date.getDate();
}elseif(cellType==HSSFCell.CELL_TYPE_STRING){
Stringdate=getStringCellValue(cell);
result=date.replaceAll("[年月]","-").replace("日","").trim();
}elseif(cellType==HSSFCell.CELL_TYPE_BLANK){
result="";
}
}catch(Exceptione){
System.out.println("日期格式不正確!");
e.printStackTrace();
}
returnresult;
}

/**
*根據HSSFCell類型設置數據
*@paramcell
*@return
*/
(HSSFCellcell){
Stringcellvalue="";
if(cell!=null){
//判斷當前Cell的Type
switch(cell.getCellType()){
//如果當前Cell的Type為NUMERIC
caseHSSFCell.CELL_TYPE_NUMERIC:
caseHSSFCell.CELL_TYPE_FORMULA:{
//判斷當前的cell是否為Date
if(HSSFDateUtil.isCellDateFormatted(cell)){
//如果是Date類型則,轉化為Data格式

//方法1:這樣子的data格式是帶時分秒的:2011-10-120:00:00
//cellvalue=cell.getDateCellValue().toLocaleString();

//方法2:這樣子的data格式是不帶帶時分秒的:2011-10-12
Datedate=cell.getDateCellValue();
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
cellvalue=sdf.format(date);

}
//如果是純數字
else{
//取得當前Cell的數值
cellvalue=String.valueOf(cell.getNumericCellValue());
}
break;
}
//如果當前Cell的Type為STRIN
caseHSSFCell.CELL_TYPE_STRING:
//取得當前的Cell字元串
cellvalue=cell.getRichStringCellValue().getString();
break;
//默認的Cell值
default:
cellvalue="";
}
}else{
cellvalue="";
}
returncellvalue;

}

publicstaticvoidmain(String[]args){
try{
//對讀取Excel表格標題測試
InputStreamis=newFileInputStream("d:\test2.xls");
ExcelReaderexcelReader=newExcelReader();
String[]title=excelReader.readExcelTitle(is);
System.out.println("獲得Excel表格的標題:");
for(Strings:title){
System.out.print(s+"");
}

//對讀取Excel表格內容測試
InputStreamis2=newFileInputStream("d:\test2.xls");
Map<Integer,String>map=excelReader.readExcelContent(is2);
System.out.println("獲得Excel表格的內容:");
for(inti=1;i<=map.size();i++){
System.out.println(map.get(i));
}

}catch(FileNotFoundExceptione){
System.out.println("未找到指定路徑的文件!");
e.printStackTrace();
}
}
}

4. java poi 讀取excel 數字類型的怎麼讀到以後1都變成了1.0

這是正常的。通過POI取出的數值默認都是double,即使excel單元格中存的是1,取出來的值也是1.0,這就造成了一些問題,如果資料庫欄位是int,那麼就會wrongdatatype,所以需要對數值類型處理。代碼如下:Cellcell=null;//單元格ObjectinputValue=null;//單元格值if(!isEmpty(cell)&&cell.getCellType()==Cell.CELL_TYPE_NUMERIC){longlongVal=Math.round(cell.getNumericCellValue());if(Double.parseDouble(longVal+".0")==doubleVal)inputValue=longVal;elseinputValue=doubleVal;}這么處理後,單元格中的小數沒有變化,如果是整數,也會取到整數。

5. poi讀取excel文件內容

//參數分別是開始行,結束行,行對應的開始列和結束列
public void getExcelElement(int startRow,int endRow,int startCell,int endCell){
XSSFWorkbook wb=new XSSFWorkbook();
XSSFSheet sheet=wb.getSheet("sheetName");
for(int i=startRow;i<=endRow;i++){
XSSFRow row=sheet.getRow(i);
for(int a=startCell;a<=endCell;a++){
XSSFCell cell=row.getCell(a);
System.out.println(cell);
}
}

}


我沒跑過你先試試有問題再說

6. java poi 讀取excel 數字類型

那就用try catch吧
try里轉化成整形,catch里轉成浮點型

7. Poi如何去讀取excel文件

直接全部在action裡面寫的,這個就不多說了,直接上代碼:

public String executeExcel() throws Exception{ String realPath = ServletActionContext.getServletContext().getRealPath("/fileupload");
System.out.println(fileFileName);
String filePath = "";
if(this.file!=null){
File saveFile = new File(new File(realPath),this.fileFileName);
filePath = realPath+"\\"+this.fileFileName;
System.out.println(filePath);
if(!saveFile.getParentFile().exists()){
saveFile.getParentFile().mkdirs(); }
FileUtils.File(file, saveFile); }
this.exlToDB(filePath);
ActionContext.getContext().put("message","導入成功");
return "success"; } //讀取excel2007,並把數據插入資料庫
public void exlToDB(String filePath){ boolean flag = true;
AllKpi akpi = new AllKpi(); try { // 文件流指向excel文件
FileInputStream fin=new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(fin);// 創建工作薄
XSSFSheet sheet = workbook.getSheetAt(0);// 得到工作表
XSSFRow row = null;// 對應excel的行
XSSFCell cell = null;// 對應excel的列
int totalRow = sheet.getLastRowNum();// 得到excel的總記錄條數
System.out.println(totalRow); // 以下的欄位一一對應資料庫表的欄位
float idd = 0.0f;
String id = "";
String Name = "";
String DEPT_NAME = "";
String Weight = "";
<span></span>String ALGORITHM = "";
String text = " ";
//String sql = "insert into DSP_TJ_KPI values(DSP_TJ_KPI_SEQ.nextval,?,?,?,'無',?)";
for (int i = 1; i <= totalRow; i++) {
row = sheet.getRow(i);
//System.out.println(row.getCell(0).toString());
if(row.getCell(0) != null && !"".equals(row.getCell(0)) && row.getCell(1) != null && !"".equals(row.getCell(1)) && row.getCell(2) != null && !"".equals(row.getCell(2)) && row.getCell(3) != null && !"".equals(row.getCell(3))){
cell = row.getCell((short) 0);
Name = cell.toString();
System.out.println(Name);
cell = row.getCell((short) 1);
Weight = cell.toString();
System.out.println(Weight);
cell = row.getCell((short) 2);
DEPT_NAME = cell.toString();
System.out.println(DEPT_NAME);
cell = row.getCell((short) 3);
ALGORITHM = cell.toString();
System.out.println(ALGORITHM);
akpi.setAllkpiName(Name);
akpi.setAllkpiDeptName(DEPT_NAME);
akpi.setAllkpiWeight(Weight);
akpi.setAlgorithm(ALGORITHM);
akpi.setText(text);
allKpiService.addAllKpi(akpi); //以下注釋代碼為連接jdbc測試代碼塊
/*pst = con.prepareStatement(sql);
//pst.setString(1, student_id);
pst.setString
(1, DEPT_NAME);
pst.setString
(2, Name);
pst.setString
(3, Weight);
<span></span>pst.setString(4, ALGORITHM);
pst.execute();*/
System.out.println("preparestatement successful"); } }
/*pst.close();
con.close();*/
fin.close(); } catch (FileNotFoundException e)
{
flag = false;
e.printStackTrace();
}
catch (IOException ex)
{
flag = false;
ex.printStackTrace();
}

閱讀全文

與POI讀取Excel文件類型相關的資料

熱點內容
有奶電影 瀏覽:646
百度移動端排名工具 瀏覽:938
安卓安裝程序不見了 瀏覽:251
3d9中文精簡版找不到文件 瀏覽:839
手機能關聯哪些app 瀏覽:423
電影播放量排行榜在哪裡看 瀏覽:717
網路銷售辦公圖片素材 瀏覽:390
ps肉色代碼 瀏覽:258
快穿以肉為主 瀏覽:376
券商的數據分析工程師怎麼樣 瀏覽:31
日本武士恐怖電影 瀏覽:120
電影電視劇小孩子的戀愛 瀏覽:383
怎麼下載pptv網路電視 瀏覽:3
邵氏論理電影 瀏覽:142
ps怎麼導成製作文件 瀏覽:273
文件夾和桌面同步快捷方式 瀏覽:187
阿里雲如何上傳壓縮文件 瀏覽:244
李彩譚作品大全 瀏覽:955
可迅雷下載的免費網站你懂得 瀏覽:951

友情鏈接