📄 excelimportimpl.java
字号:
package org.pontifex.commons.exchange;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by IntelliJ IDEA.<br>
* User: Songzou <br>
* Date: 2007-05-10 <br>
* Time: 11:57:43 <br>
*/
public class ExcelImportImpl implements ExcelImport {
private HSSFWorkbook workbook;
private DiskFileUpload fu ;
private HttpServletRequest request ;
private ServletContext appliaction ;
private Map parameter;
private static final int MAX_SIZE = 4 * 1024 * 1024; // 设置最大文件尺寸,这里是4MB
private static final int BUFFER_SIZE = 4 * 1024; // 设置缓冲区大小,这里是4kb
private static final String FILE_TEMP_PATH = "\\WEB-INF\\temp"; // 设置临时上传目录:
// private static final String FILE_SAVE_PATH = "\\WEB-INF\\upload"; // 设置
private ExcelImportImpl() {
parameter = new HashMap();
}
public ExcelImportImpl(ServletContext appliaction,HttpServletRequest request ) throws IOException, ServletException {
this();
this.request = request ;
this.appliaction = appliaction;
init();
}
public void init(){
parameter = new HashMap();
String realpath = appliaction.getRealPath("/");
fu = new DiskFileUpload();
fu.setSizeMax(MAX_SIZE);
fu.setSizeThreshold(BUFFER_SIZE);
fu.setRepositoryPath(realpath + FILE_TEMP_PATH);
// System.out.println("文件上传临时路径:"+fu.getRepositoryPath());
}
public boolean readExcel1() throws Exception{
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
if(i.hasNext()) {
FileItem fi = (FileItem)i.next();
// 获得文件名,这个文件名包括路径:
String fileName = fi.getName();
if(fileName!=null) {
System.out.println("文件名:"+fileName);
// 在这里可以记录用户和文件信息
// ...
// 写入文件a.txt,你也可以从fileName中提取文件名:
InputStream inputStream = null;
try{
inputStream = fi.getInputStream();
workbook = new HSSFWorkbook(inputStream);
// fi.write(new File(appliaction.getRealPath("/")+FILE_SAVE_PATH+"/"+new java.util.Date().getTime()+".xls"));
return validate();
}finally{
if(inputStream!=null)inputStream.close();
}
}
}
return false;
}
public boolean readExcel()throws IOException{
InputStream inputStream = null;
File file = new File("e:\\新建 文本文档.txt");
try{
if(file.exists()){
inputStream = new FileInputStream(file);
workbook = new HSSFWorkbook(inputStream);
// fi.write(new File(appliaction.getRealPath("/")+FILE_SAVE_PATH+"/"+new java.util.Date().getTime()+".xls"));
return validate();
}
return false;
}finally{
if(inputStream!=null)inputStream.close();
}
}
public boolean validate(){
int num = workbook.getNumberOfSheets();
for(int i=0;i<num;i++){
System.out.println("第"+i+"页");
String sheetName = workbook.getSheetName(i);
System.out.println("sheetName:"+sheetName);
HSSFSheet hSSFSheet = workbook.getSheetAt(i);
System.out.println("getPhysicalNumberOfRows:"+hSSFSheet.getPhysicalNumberOfRows());
for(int j = hSSFSheet.getFirstRowNum();j<=hSSFSheet.getLastRowNum();j++){
System.out.println("第"+j+"行");
HSSFRow hSSFRow = hSSFSheet.getRow(j);
if(hSSFRow==null)continue;
for(short k = hSSFRow.getFirstCellNum();k<hSSFRow.getLastCellNum();k++){
HSSFCell hSSFCell = hSSFRow.getCell(k);
if(hSSFCell==null){
System.out.print("\t");
continue;
}
Object value = null;
switch(hSSFCell.getCellType()){
case HSSFCell.CELL_TYPE_STRING:
value = hSSFCell.getStringCellValue();break;
case HSSFCell.CELL_TYPE_BLANK:
value = hSSFCell.getStringCellValue();break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = hSSFCell.getBooleanCellValue();break;
case HSSFCell.CELL_TYPE_ERROR:
value = hSSFCell.getErrorCellValue();break;
case HSSFCell.CELL_TYPE_FORMULA:
value = hSSFCell.getCellFormula();break;
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(hSSFCell)){
value = hSSFCell.getDateCellValue();
}else{
value = hSSFCell.getNumericCellValue();
}
break;
default :break;
}
if(value instanceof Date){
System.out.print("a "+new SimpleDateFormat("yyyy-MM-dd").format(value)+"\t");
}else{
System.out.print(value+"\t");
}
}
System.out.println();
}
System.out.println();
}
return true;
}
public static void main(String[] args) throws IOException{
ExcelImport excelImport = new ExcelImportImpl();
excelImport.readExcel();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -