/**
* 导入excel方法调用
*/
public void importExcel(HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
if(items.size()>0&&!"".equals(inspectTypeId)) {
FileItem fi = items.get(0);
InputStream is=fi.getInputStream();
BufferedInputStream in = new BufferedInputStream(is);
}
JsonUtil.ajaxJsonMessage(response, "导入成功",Message.SUCCESS);
} catch (Exception e) {
e.printStackTrace();
}
}/**
* 导出excel* 先循环创建表头所有的单元格列,并设置边框样式,再根据行号,列号设置特定单元的值,
* 最后才是合并,保证边框不** 丢失
*/
public void exportExcel(HttpServletRequest request,
HttpServletResponse response) throws Exception {HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setWrapText(true);//指定当单元格内容显示不下时自动换行
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//指定单元格居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //指定单元格垂直居中对齐
HSSFSheet sheet = wb.createSheet(“模板”);
//创建行
HSSFRow row=sheet.createRow(0);HSSFCell cell = row.createCell(0);//创建单元格
cell.setCellValue(filedDec);//赋值
//合并单元格,起始行 起始列 结束行 结束列
CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,1,3);
sheet.addMergedRegion(callRangeAddress);
this.setBorderForMergeCell(1, callRangeAddress, sheet,wb) ;
cell.setCellStyle(cellStyle);
String filename=URLEncoder.encode(String.valueOf(System.currentTimeMillis()), "UTF-8")+".xls";
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename=\"" + filename + "\"");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream output = response.getOutputStream();
try {
wb.write(output);
output.flush();
} finally{
if(output!=null){
output.close();
}
}
}
/**
合并单元格加边框
**/
private void setBorderForMergeCell(int i,
CellRangeAddress callRangeAddress,
HSSFSheet sheet,
HSSFWorkbook workBook){
RegionUtil.setBorderBottom(1, callRangeAddress, sheet,workBook);
RegionUtil.setBorderLeft(1, callRangeAddress, sheet,workBook);
RegionUtil.setBorderRight(1, callRangeAddress, sheet,workBook);
RegionUtil.setBorderTop(1, callRangeAddress, sheet,workBook);
}
poi导入导出excel
最新推荐文章于 2024-09-03 20:52:56 发布