java讀取 Excell資料(不包括合併表格)
讀取excell檔案資料方法
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ReadExcelUtils {
private Logger logger = LoggerFactory.getLogger(ReadExcelUtils.class);
private Workbook wb;
private Sheet sheet;
private Row row;
public ReadExcelUtils(String filepath) {
//判斷路徑
return;
}
String ext = filepath.substring(filepath.lastIndexOf("."));
try {
InputStream is = new FileInputStream(filepath);
if(".xls".equals(ext)){
wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(ext)){
wb = new XSSFWorkbook(is);
}else{
wb=null;
}
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException", e);
} catch (IOException e) {
logger.error("IOException", e);
}
}
/**
* 讀取Excel表格表頭的內容
*
* @param InputStream
* @return String 表頭內容的陣列
* @author zengwendong
*/
public String[] readExcelTitle() throws Exception{
if(wb==null){
throw new Exception("Workbook物件為空!");
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 標題總列數
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
String[] title = new String[colNum];
Object obj=null;
for (int i = 0; i < colNum; i++) {
System.out.println(row.getCell(i));
obj=row.getCell(i).toString();
title[i]= (String) obj;
obj=null;
}
return title;
}
/**
* 計算列數
*/
public int colNum(){
sheet = wb.getSheetAt(0);
// 得到總行數
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells(); //列
return colNum;
}
/**
* 讀取Excel資料內容
*
* @param InputStream
* @return Map 包含單元格資料內容的Map物件
* @author zengwendong
*/
public Map<Integer, Map<Integer,Object>> readExcelContent() throws Exception{
if(wb==null){
throw new Exception("Workbook物件為空!");
}
Map<Integer, Map<Integer,Object>> content = new HashMap<Integer, Map<Integer,Object>>();
sheet = wb.getSheetAt(0);
// 得到總行數
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells(); //列
// 正文內容應該從第二行開始,第一行為表頭的標題
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
Map<Integer,Object> cellValue = new HashMap<Integer, Object>();
while (j < colNum) {
Object obj = row.getCell(j);//getCellFormatValue(row.getCell(j));
cellValue.put(j, obj);
j++;
}
content.put(i, cellValue);
}
return content;
}
}
Controller方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String filepath = "D:/20171101-2.xlsx"; //路徑
ReadExcelUtils excelReader = new ReadExcelUtils(filepath);
// 對讀取Excel表格內容測試
//取表頭
String[] a =excelReader.readExcelTitle();
String top=null;
for(int i=0;i<1;i++){
top=a[i];//現在只能取一個表頭的,原因這個例子不同意讓我往下寫了 ,就先放下了 。
}
List<String> list=new ArrayList<String>();//裝正文內容
int colnum =excelReader.colNum();//呼叫方法獲取列數。
Map<Integer, Map<Integer,Object>> map = excelReader.readExcelContent(); //獲取正文。
System.out.println("獲得Excel表格的內容:");
//從報表的第二行開始取 (i = 1)
for (int i = 1; i <= map.size(); i++) {
for(int j=0;j<colnum;j++){
//每行的。 每列的。 放list集合裡。
list.add( map.get(i).get(j).toString());
}
}
//傳給前臺
request.setAttribute("top", top);
request.setAttribute("colnum",colnum);
request.setAttribute("list",list);
//前臺路徑
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (FileNotFoundException e) {
System.out.println("未找到指定路徑的檔案!");
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
index.jsp
<table>
<tr><td>${top}</td></tr><!-- 頭標題 -->
<c:forEach var="list1" items="${list}" varStatus="status">
<!-- 第一次的時候寫一個tr頭 -->
<c:if test="${status.count==0}">
<tr>
</c:if>
<!-- td內容 目前是 input接收 這個可以換 成div 什麼的 -->
<td><input value="${list1}"></td>
<!-- varStatus等於列數時或可以整除列數時 寫一個結束tr -->
<c:if test="${status.count==colnum||status.count%colnum==0}">
</tr>
</c:if>
</c:forEach>
</table>
轉載地址:http://blog.csdn.net/superdog007/article/details/48338337
jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
CSDN 獲取jar包 http://download.csdn.net/download/qq_27925345/10233391