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
- java操作Excel工具类简易版
- java操作Excel工具类
- java操作excel的工具
- java简易excel导入导出工具(封装POI)
- java简易excel导入导出工具(封装POI)
- ExcelUtil工具类,使用Java代码操作Excel表格
- POI工具类,常用java操作导出Excel
- Java实现对Excel表格的操作工具类
- java excel工具类
- Excel操作工具类ExcelHelper
- poi操作excel工具类
- 【工具类】常用操作Excel
- java操作excel的工具jxl
- Java script操作Excel报表工具汇总
- JAVA通过JXL工具操作EXCEL文件
- Java Excel操作工具类,创建一个Excel,只需一行代码。
- Java导出Excel工具类
- Java导出Excel工具类
- 1029. Median (25)
- 海思u-boot.bin
- asp.NET 如何生成DLL文件,并调用
- javascript,js 子窗口向父窗口赋值
- JavaBridge 与 Tomcat8
- java操作Excel工具类简易版
- win7 纯净版
- 1114
- 内核内存检测
- 一股热流流变了我的全身
- SQLLite教程基础篇
- 本该到客运中心的却到了西站
- 接口的好处与实例操作
- 们无辜地对茂密的森林乱砍滥伐