解析Excel資料

2022-02-04 19:10:31 字數 1603 閱讀 3410

解析excel資料常用的方式就是使用poi和jxl工具了,這兩種工具使用起來有些類似,這裡記錄一下使用方式提供個參考

file file = new

file(filepath);

fileinputstream fis = new

fileinputstream(file);

workbook =workbookfactory.create(fis);

使用以下方式建立workbook

//workbook = new hssfworkbook(fis);

//.xls格式

int sheetnum = workbook.getnumberofsheets();//

sheet的數量

system.out.println("共[" + sheetnum + "]個工作表");

for (int i = 0; i < sheetnum; i++)

使用以下方式判斷單元格型別

/*switch (cell.getcelltype())

*/system.out.printf("每個單元格的值:"+cellvalue);}}

}

注意poi版本使用上有些許的不同,poi3.6及以上.xls和.xlsx格式的excel對應的是不同的workbook 實現類, hssfworkbook只能操作excel2003以下版本(.xls),xssfworkbook只能操作excel2007以上版本(.xlsx),並且判斷單元格型別使用的方法也有些許不同,需要注意下。

workbook book = workbook.getworkbook(new

file(filepath));

//獲得第乙個**物件

sheet sheet = book.getsheet(0); //這裡0代表只

解析第乙個sheet頁

//拿到**的行數

int row =sheet.getrows();

//拿到**的列數

int col =sheet.getcolumns();

for (int j = 0; j < col; j++)

/*//判斷單元格型別

celltype celltype = cell.gettype();

if(celltype == celltype.empty)else if(celltype == celltype.boolean)else if(celltype == celltype.boolean_formula)else if(celltype == celltype.date)else if(celltype == celltype.date_formula)else if(celltype == celltype.number)else if(celltype == celltype.number_formula)else if(celltype == celltype.string_formula)else if(celltype == celltype.error)else if(celltype == celltype.formula_error)else element.setvalue(cell.getcontents());

*/

Excel解析入庫

1.配置資料庫連線字串 1 sqlserver資料庫連線 2 oracle資料庫連線 2.獲取資料庫連線字串 sqlserver sql.cs private static string connstr configurationmanager.connectionstrings sql conne...

上傳解析Excel

專案中上傳解析excel的功能,非常普遍,因為方便,接下來就寫乙個工具類對excel進行解析 引入poi 阿帕奇依賴 工具類的書寫public class excelutils else if excel 2007 suffix.equalsignorecase suffixname catch i...

上傳excel檔案,解析excel檔案

需要jxl.jar包 上傳excel檔案 獲取檔案字尾名 public static string getfileext string f return param excelfile 讀取檔案物件 param rownum 從第幾行開始讀,如果有一行表頭則從第二行開始讀 return throws...