java操作Excel工具类简易版

来源:互联网 发布:js获取div的class 编辑:程序博客网 时间:2024/05/23 19:28
package com.newsky.action;import com.newsky.domain.DmYMD;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.struts2.StrutsStatics;public class ExportExcel extends ActionSupport {/** *  */private static final long serialVersionUID = 1L;public static InputStream getInputStream(List<DmYMD> list) throws IOException {HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("sheet1");// String sheet = new String(sheet.getBytes("ISO-8859-1"), "UTF-8");//int rowNum = 0; // 标题开始行int colNum = 0; // 标题开始列// HSSFRow excell的头HSSFRow row = sheet.createRow(rowNum++);// HSSFCell excell的格子单元HSSFCell cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue("时间");cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue("点数");cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue("播数");cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue("包数");cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue("月数");cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue("额数");if (null == list || list.size() == 0) {} else {for (DmYMD user : list) {row = sheet.createRow(rowNum++);colNum = 0;cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue(user.getQueryTime());cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue(Double.parseDouble(user.getDbyhs()));cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue(Double.parseDouble(user.getDbs()));cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue(Double.parseDouble(user.getByyhs()));cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue(Double.parseDouble(user.getBys()));cell = row.createCell((short) colNum++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;cell.setCellValue(Double.parseDouble(user.getDbfee()));}}HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);// request.setCharacterEncoding("UTF-8");String realSavePath = request.getSession().getServletContext().getRealPath("/upload");System.out.println(realSavePath);File file = new File(realSavePath + "\\report.xls");// File file = new File("c:\\Company.xls");// 创建一个File 拿来当缓存用.也就是先将内存中的excel写入File中.然后再将File转换成输出流OutputStream out = null;try {out = new FileOutputStream(file);wb.write(out);// 写入File//out.close();out.flush();} catch (Exception e) {e.printStackTrace();} finally {if (out != null) {out.close();}}InputStream in = null;try {in = new FileInputStream(file);// 将file转换成输入流// in.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(in!=null){in.close();}}return in;}}

此处中文没有做统一处理,无cell.setEncoding(HSSFCell.ENCODING_UTF_16);会下载下来中文乱码


/** *  */package com.newsky.action;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.StrutsStatics;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;/** * @author HUXU *  */public class DownReport extends ActionSupport {/** *  */private static final long serialVersionUID = 1L;public InputStream getInputStream() {HttpServletRequest request = (HttpServletRequest) ActionContext  .getContext().get(StrutsStatics.HTTP_REQUEST);//  request.setCharacterEncoding("UTF-8");          String realSavePath = request.getSession().getServletContext().getRealPath("/upload");          File file = new File(realSavePath + "\\report.xls");//File file = new File("c:\\Company.xls");// 创建一个File 拿来当缓存用.也就是先将内存中的excel写入File中.然后再将File转换成输出流InputStream in = null;if (file.exists()) {try {in = new FileInputStream(file);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 将file转换成输入流} else {try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("文件不存在");}return in;}public String execute() throws Exception {try {getInputStream();return SUCCESS;} catch (Exception e1) {e1.printStackTrace();return ERROR;}}}

以上的下载名称应该是用当前时间,此处都写成了export.xls