废话不多说、上代码
导入数据表
package com.uitl;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
-
被解析的Excel最好是什么样的呢?
-
单元格最好都是文本格式,保存数据前自己去转换,不用poi带的转换。
-
第一列 和最后一列 必须是必填字段!!!这样的你用我这个Util,得到的List就很准确了,不会出现多余的行或列。
-
@author
-
@version
-
提示:
-
目前只支持2003版的Excel文件的导入操作
*/
public class ImportExcelUtil {
private final static String excel2003L =".xls"; //2003- 版本的excel
//private final static String excel2007U =".xlsx"; //2007+ 版本的excelstatic SimpleDateFormat sFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
static short[] yyyyMMdd = {14, 31, 57, 58, 179, 184, 185, 186, 187, 188};
static short[] HHmmss = {20, 32, 190, 191, 192};
static List<short[]> yyyyMMddList = Arrays.asList(yyyyMMdd);
static List<short[]> hhMMssList = Arrays.asList(HHmmss);
/*-
测试代码
/
public static void main(String args[]){
try{
InputStream in=new FileInputStream(“f:/stu.xls”);
ImportExcelUtil ieu=new ImportExcelUtil();
List<List> res=ieu.getBankListByExcel(in, “.xls”);
for(int i=0;i<res.size();i++)
System.out.println(res.get(i).get(0)+"---->"+res.get(i).get(1));
}catch(Exception e){
e.printStackTrace();
}
}
/* -
描述:获取IO流中的数据,组装成List<List>对象
-
@param in,fileName
-
@return
-
@throws IOException
*/
public List<List> getBankListByExcel(InputStream in,String fileType) throws Exception{
List<List> list = null;//创建Excel工作薄
Workbook work = this.getWorkbook(in,fileType);
if(null == work){
throw new Exception(“创建Excel工作薄为空!”);
}
Sheet sheet = null;
Row row = null;
Cell cell = null;list = new ArrayList<List>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheetnull){continue;}
int totalCell = sheet.getRow(0).getPhysicalNumberOfCells(); //标题行一共有多少列
//遍历当前sheet中的所有行
for (int j = sheet.getFirstRowNum()+1; j < sheet.getLastRowNum()+1; j++) {
row = sheet.getRow(j);
if(rownull || validateRow(row) || row.getPhysicalNumberOfCells() < totalCell){continue;} //3个条件,有一个为true就不会往list里加,不仅过滤空行还过滤了列数不够的行,这点要注意,要求表中前后的列都是必填的。
//遍历所有的列
List li = new ArrayList();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
li.add(this.getCellData(cell));
}
list.add(li);
}
// 简单起见,这里只解析第一个工作簿!
break;
}
//work.close();
return list;
}
// 过滤空行,(其中一行的数据的确都为空,可是其原本的格式还在,并没有连带删除,这样计算出来的行数就不真实,比真实的大)
private boolean validateRow(Row row) throws Exception{
// for (Cell cell : row) {
//
// }
//只判断第一列。第一列为空就代表这行的数据无效
if (row.getCell(0).getCellType() == Cell.CELL_TYPE_BLANK || “”.equals(this.getCellData(row.getCell(0)))) {
return true;
}
return false;//不是空行
}
/** -
描述:根据文件后缀,自适应上传文件的版本
-
@param inStr,fileName
-
@return
-
@throws Exception
/
public Workbook getWorkbook(InputStream inStr,String fileType) throws Exception{
Workbook wb = null;
if(excel2003L.equals(fileType)){
wb = new HSSFWorkbook(inStr); //2003-
}
/- else if(excel2007U.equals(fileType)){
wb = new XSSFWorkbook(inStr); //2007+
}
*/
else{
throw new Exception(“解析的文件格式有误!”);
}
return wb;
}
- else if(excel2007U.equals(fileType)){
/**
-
获取单元中值(字符串类型)
-
@param cell
-
@return
-
@throws Exception
*/
public String getCellData(Cell cell) throws Exception {
String cellValue = “”;
if (cell != null) {
try {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK://空白
cellValue = “”;
break;
case Cell.CELL_TYPE_NUMERIC: //数值型 0----日期类型也是数值型的一种
if (DateUtil.isCellDateFormatted(cell)) {
short format = cell.getCellStyle().getDataFormat();if (yyyyMMddList.contains(format)) { sFormat = new SimpleDateFormat("yyyy-MM-dd"); } else if (hhMMssList.contains(format)) { sFormat = new SimpleDateFormat("HH:mm:ss"); } Date date = cell.getDateCellValue(); cellValue = sFormat.format(date); } else { cell.setCellType(Cell.CELL_TYPE_STRING); cellValue = replaceBlank(cell.getStringCellValue()); //Double numberDate = new BigDecimal(cell.getNumericCellValue()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();//似乎还是有点问题 //cellValue = numberDate + ""; } break; case Cell.CELL_TYPE_STRING: //字符串型 1 cellValue = replaceBlank(cell.getStringCellValue()); break; case Cell.CELL_TYPE_FORMULA: //公式型 2 cell.setCellType(Cell.CELL_TYPE_STRING); cellValue = replaceBlank(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: //布尔型 4 cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: //错误 5 cellValue = "!#REF!"; break; } } catch (Exception e) { throw new Exception("读取Excel单元格数据出错:" + e.getMessage()); }
}
return cellValue;
}
public static String replaceBlank(String source) {
String dest = “”;
if (source != null) {
Pattern p = Pattern.compile("\t|\r|\n");
Matcher m = p.matcher(source);
dest = m.replaceAll("");
}
return dest.trim();
} -
}
导出数据表
package com.uitl;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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;
import org.apache.poi.hssf.util.HSSFColor;
/**
-
通用的导出Excel类,如果需要自定义格式的,参照此类自己再写类或方法来实现,
-
dataList里的每一个Object数组一个元素(object[0])都是序号,不可放真实数据
-
提示:此类对导出的Excel表格未格式化处理
-
@author
*/
public class ExportExcelUtil {
private String title; // 导出表格的表名
private String[] rowName;// 导出表格的列名
private List<Object[]> dataList = new ArrayList<Object[]>(); // 对象数组的List集合
private HttpServletResponse response;
private HttpServletRequest request;/**
- 实例化导出类
- @param title 导出表格的表名,最好是英文,中文可能出现乱码
- @param rowName 导出表格的列名数组
- @param dataList 对象数组的List集合
- @param response
*/
public ExportExcelUtil(String title,String[] rowName,List<Object[]> dataList, HttpServletRequest request, HttpServletResponse response){
this.title=title;
this.rowName=rowName;
this.dataList=dataList;
this.response = response;
this.request = request;
}
// 导出数据
public void exportData() throws Exception{
HSSFWorkbook workbook =new HSSFWorkbook(); // 创建一个excel对象
HSSFSheet sheet =workbook.createSheet(title); // 创建表格//sheet.setDefaultRowHeightInPoints(18.5f); // sheet样式定义,调用下面定义的三个方法 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook,16); // 头样式(标题) //HSSFCellStyle columnStyle = this.getColumnStyle(workbook,11); // 标题样式,加粗效果 HSSFCellStyle columnStyle = this.getStyle(workbook,11); // 标题样式. 不加粗 HSSFCellStyle style = this.getStyle(workbook,11); // 单元格样式 //sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (rowName.length-1))); /* * 合并第一行的所有列 * 产生表格标题行 HSSFRow rowm =sheet.createRow(0); // 行 rowm.setHeightInPoints(26f); HSSFCell cellTiltle =rowm.createCell(0); // 单元格 cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); */ int columnNum = rowName.length; // 表格列的长度 HSSFRow rowRowName = sheet.createRow(0); // 在第一行创建行 HSSFCellStyle cells =workbook.createCellStyle(); cells.setBottomBorderColor(HSSFColor.BLACK.index); rowRowName.setRowStyle(cells); // 循环 将列名放进去 for (int i = 0; i < columnNum; i++) { HSSFCell cellRowName = rowRowName.createCell(i); cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 单元格类型 HSSFRichTextString text = new HSSFRichTextString(rowName[i]); // 得到列的值 cellRowName.setCellValue(text); // 设置列的值 cellRowName.setCellStyle(columnStyle); // 样式 } // 将查询到的数据设置到对应的单元格中 for (int i = 0; i < dataList.size(); i++) { Object[] obj = dataList.get(i); //遍历每个对象 HSSFRow row = sheet.createRow(i+1); //创建所需的行数 for (int j = 0; j < obj.length; j++) { HSSFCell cell = null; //设置单元格的数据类型 /* * 第一种情况:若想给每行指定序号,则按一下的代码来实现 * if(j==0){ // 第一列设置为序号 cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 }else{ cell.setCellValue(" "); } } */ /* * 第二种情况:直接按显示的表格记录列的顺序导出来 */ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 }else{ cell.setCellValue(" "); } cell.setCellStyle(style); // 样式 } } // 让列宽随着导出的列长自动适应,但是对中文支持不是很好,也可能在linux(无图形环境的操作系统)下报错,报错再说 for (int i = 0; i < columnNum; i++) { sheet.autoSizeColumn(i); sheet.setColumnWidth(i, sheet.getColumnWidth(i)+888);//适当再宽点 } if(workbook !=null){ /* * 导出方式1:输出到服务器上指定的文件中 FileOutputStream fileOutputStream = new FileOutputStream("D:/user.xls"); workbook.write(fileOutputStream);//将数据写出去 fileOutputStream.close();//关闭输出流 */ /*导出方式2: 输出到用户浏览器上 * */ OutputStream out = response.getOutputStream(); try { //指定保存的excel 表文件名,则给定的名称后面加上当前系统的日期时间 String fileName = title + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ".xls"; String fileName11 = ""; String userAgent = request.getHeader("USER-AGENT"); /* * 指定不同浏览器中保存时编码方式 */ if(StringUtils.contains(userAgent, "Firefox") || StringUtils.contains(userAgent, "firefox")){//火狐浏览器 fileName11 = new String(fileName.getBytes(), "ISO8859-1"); }else{ fileName11 = URLEncoder.encode(fileName,"UTF-8"); //其他浏览器 } String headStr = "attachment; filename=\"" + fileName11 + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", headStr); workbook.write(out); out.flush(); //workbook.close(); } catch (Exception e) { throw e; } finally { if (null != out) { out.close(); } } }
}
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook,int fontSize) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)fontSize);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName(“宋体”);
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}public HSSFCellStyle getColumnStyle(HSSFWorkbook workbook,int fontSize) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)fontSize);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName(“宋体”);
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}public HSSFCellStyle getStyle(HSSFWorkbook workbook,int fontSize) {
//设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)fontSize);
//字体加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName(“宋体”);
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);return style;
}
}
作者技术博客:
https://lsson.xyz/myblogs
如需联系欢迎加q群:
855473670
481557406