poi导出生成excel文件名称|java web poi如何按查询结果导出相应的Excel最好有相应代码实例

① java web poi如何按查询结果导出相应的Excel最好有相应代码实例。

package com.aerolink.aocs.util.fileUtil;import java.io.FileOutputStream;import java.io.IOException;import java.util.Calendar;import java.util.Date;//import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFCellStyle;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class WriteExcelNew { /** * Excel文件 */ private XSSFWorkbook wb = null; /** * 输出Excel文件中的表对象 */ private XSSFSheet sheet = null; /** * 输出文件流 */ private FileOutputStream fileOut = null; /** * 输出文件名用户自定义 */ private String outputFilename = null; /** * 单元格样式 */ private XSSFCellStyle cellStyle = null; // private String newsheet = null; //输出Excel文件中的表名用户自定义 /** * 行 */ private XSSFRow row=null; /** * */ private int rowNumber=-1; /** * @param outputFilename * @param newsheet */ public WriteExcelNew(String outputFilename, String newsheet) { wb = new XSSFWorkbook(); //wb.setSheetName(1, "qwe");//设置第一张表的名称 sheet = wb.createSheet(newsheet); //sheet.setColumnWidth(1, 40);//第一行 列宽 this.outputFilename = outputFilename; // this.newsheet = newsheet; } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,int value)方法: * </p> * <p> * 将int数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, int value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } //row.setHeight((short)50);//行高 XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,String value)方法: * </p> * <p> * 将String数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, String value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,double value)方法: * </p> * <p> * 将double数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, double value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,boolean * value)方法: * </p> * <p> * 将boolean数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, boolean value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,Date value)方法: * </p> * <p> * 将Date数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, Date value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,Calendar * value)方法: * </p> * <p> * 将Calendar数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, Calendar value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); } /** * 合并单元格 2012-3-30 孙贵春 add * @param startRow * @param startCol * @param endRow * @param endCol */ public void setMergedRegion(int startRow,int startCol,int endRow,int endCol){ sheet.addMergedRegion(new CellRangeAddress(startRow,endRow,startCol,endCol));//这与1.5版本明显不同 } /** * 设置单元格样式 * @param a_pos * @param v_pos * @param border * @param wrap */ public void setCellStyle(String a_pos,String v_pos,String border,Boolean wrap){ cellStyle=wb.createCellStyle(); //水平位置 if(a_pos.equals("center")){ cellStyle.setAlignment(CellStyle.ALIGN_CENTER); }else if(a_pos.equals("left")){ cellStyle.setAlignment(CellStyle.ALIGN_LEFT); }else{ cellStyle.setAlignment(CellStyle.ALIGN_RIGHT); } //垂直位置 if(v_pos.equals("center")){ cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); }else if(v_pos.equals("top")){ cellStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP); }else{ cellStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM); } //边框 if(border.equals("thin")){ cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); }else if(border.equals("double")){ cellStyle.setBorderBottom(CellStyle.BORDER_DOUBLE); cellStyle.setBorderLeft(CellStyle.BORDER_DOUBLE); cellStyle.setBorderRight(CellStyle.BORDER_DOUBLE); cellStyle.setBorderTop(CellStyle.BORDER_DOUBLE); }else if(border.equals("thick")){ cellStyle.setBorderBottom(CellStyle.BORDER_THICK); cellStyle.setBorderLeft(CellStyle.BORDER_THICK); cellStyle.setBorderRight(CellStyle.BORDER_THICK); cellStyle.setBorderTop(CellStyle.BORDER_THICK); }else if(border.equals("medium")){ cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM); cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM); cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM); cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM); }else if(border.equals("none")){ cellStyle.setBorderBottom(CellStyle.BORDER_NONE); cellStyle.setBorderLeft(CellStyle.BORDER_NONE); cellStyle.setBorderRight(CellStyle.BORDER_NONE); cellStyle.setBorderTop(CellStyle.BORDER_NONE); }else if(border.equals("dotted")){ cellStyle.setBorderBottom(CellStyle.BORDER_DOTTED); cellStyle.setBorderLeft(CellStyle.BORDER_DOTTED); cellStyle.setBorderRight(CellStyle.BORDER_DOTTED); cellStyle.setBorderTop(CellStyle.BORDER_DOTTED); } //设置自动换行 cellStyle.setWrapText(wrap); /* cellStyle.setRotation((short)90);//设置单元格内文字旋转角度 XSSFFont font=wb.createFont(); font.setFamily(1); font.setBold(true); cellStyle.setFont(font); */ } /** * <p> * Description: closeFileOut()方法:关闭文件输出流 * </p> * * @throws IOException */ public void closeFileOut() throws IOException { fileOut = new FileOutputStream(outputFilename); wb.write(fileOut); fileOut.close(); } /** * <p> * 测试方法 * </p> * * @param arg */ public static void main(String arg[]) { try { WriteExcelNew writeExcel = new WriteExcelNew("bak\\new.xls", "newsheet"); writeExcel.exportToExcelFile((short) 0, (short) 0, 99.99); writeExcel.closeFileOut(); } catch (Exception e) { System.out.println("Something Happen"); e.printStackTrace(); } }}

② 如何使用POI对Excel表进行导入和导出

导入POI的jar包新建一个项目,在根目录在新建一个lib文件夹,将jar包复制粘贴到lib文件夹后,右键将其添加到项目的build path中,最后的结果如图所示:2编写java类,新建一个实体类,比如我们要导出数据库的有关电脑的信息,那么就建一个Computer实体类,代码如下:package com.qiang.poi;public class Computer {private int id;private String name;private String description;private double price;private double credit;public int getId() {return id;}public Computer(int id, String name, String description, double price,double credit) {super();this.id = id;this.name = name;this.description = description;this.price = price;this.credit = credit;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public double getCredit() {return credit;}public void setCredit(double credit) {this.credit = credit;}}3新建一个写入excel的方法,如write2excel,参数可以后面边写边决定(站在一个不熟悉POI的角度)public static void write2Excel(){} 4创建操作Excel的HSSFWorkbook对象HSSFWorkbook excel= new HSSFWorkbook();创建HSSFSheet对象Excel中的一个sheet(工作表)对应着java中的一个HSSFSheet对象,利用HSSFWorkbook对象可以创建一个HSSFSheet对象如:创建一个sheet名为computer的excel HSSFSheet sheet = excel.createSheet("computer");创建第一行标题信息的HSSFRow对象我们都知道excel是表格,即由一行一行组成的,那么这一行在java类中就是一个HSSFRow对象,我们通过HSSFSheet对象就可以创建HSSFRow对象如:创建表格中的第一行(我们常用来做标题的行) HSSFRow firstRow = sheet.createRow(0); 注意下标从0开始创建标题行中的HSSFCell数组当然,excel中每一行是由若干个单元格,我们常称为cell,它对应着java中的HSSFCell对象如:创建5个单元格 HSSFCell cells[] = new HSSFCell[5]; //假设我们一行有五列数据创建标题数据,并通过HSSFCell对象的setCellValue()方法对每个单元格进行赋值既然单元格都准备好了,那最后是不是该填充数据了呀。对的,没错。填充数据之前,得把数据准备好吧,数据:String[] titles = new String[]{"id","name","description","price","credit"};插入一句话: 在这个时代,能让机器做的,尽量不让人来做,记住这句话。好的,继续。现在就通过for循环来填充第一行标题的数据for (int i = 0; i < 5; i++) {cells[0] = firstRow.createCell(i);cells[0].setCellValue(titles[i]);}数据分析第一行标题栏创建完毕后,就准备填充我们要写入的数据吧,在java中,面向对象给我们带来的好处在这里正好体现了,没错把要填写的数据封装在对象中,即一行就是一个对象,n行就是一个对象列表嘛,好的,走起。创建对象Computer,私有属性id,name,description,price,credit,以及各属性的setter和getter方法,如步骤二所示。假设我们要写入excel中的数据从数据库查询出来的,最后就生成了一个List<Computer>对象computers数据写入具体数据有了,又该让机器帮我们干活了,向excel中写入数据。for (int i = 0; i < computers.size(); i++) {HSSFRow row = sheet.createRow(i + 1);Computer computer = computers.get(i);HSSFCell cell = row.createCell(0);cell.setCellValue(computer.getId());cell = row.createCell(1);cell.setCellValue(computer.getName());cell = row.createCell(2);cell.setCellValue(computer.getDescription());cell = row.createCell(3);cell.setCellValue(computer.getPrice());cell = row.createCell(4);cell.setCellValue(computer.getCredit());}将数据真正的写入excel文件中做到这里,数据都写好了,最后就是把HSSFWorkbook对象excel写入文件中了。OutputStream out = null;try {out = new FileOutputStream(file);excel.write(out);out.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("数据已经写入excel"); //温馨提示看看我的main方法吧public static void main(String[] args) throws IOException {File file = new File("test1.xls");if(!file.exists()){file.createNewFile();}List<Computer> computers = new ArrayList<Computer>();computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));write2excel(computers, file);}工程目录及执行main方法后的test1.xls数据展示源码分享,computer就不贴了package com.qiang.poi;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;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;public class ReadExcel {public static void main(String[] args) throws IOException {File file = new File("test1.xls");if(!file.exists()){file.createNewFile();}List<Computer> computers = new ArrayList<Computer>();computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));write2excel(computers, file);}public static void write2excel(List<Computer> computers,File file) {HSSFWorkbook excel = new HSSFWorkbook();HSSFSheet sheet = excel.createSheet("computer");HSSFRow firstRow = sheet.createRow(0);HSSFCell cells[] = new HSSFCell[5];String[] titles = new String[] { "id", "name", "description", "price","credit" };for (int i = 0; i < 5; i++) {cells[0] = firstRow.createCell(i);cells[0].setCellValue(titles[i]);}for (int i = 0; i < computers.size(); i++) {HSSFRow row = sheet.createRow(i + 1);Computer computer = computers.get(i);HSSFCell cell = row.createCell(0);cell.setCellValue(computer.getId());cell = row.createCell(1);cell.setCellValue(computer.getName());cell = row.createCell(2);cell.setCellValue(computer.getDescription());cell = row.createCell(3);cell.setCellValue(computer.getPrice());cell = row.createCell(4);cell.setCellValue(computer.getCredit());}OutputStream out = null;try {out = new FileOutputStream(file);excel.write(out);out.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

③ 关于POI运用于WEB导出Excel,出现文件格式或者文件扩展名无效

1.调用win+R的快捷键运行。输入regedit并单击ok。

④ easy-poi导出Excel文件,默认文件名不对,怎么修改

不知道是不是你上图空格的原因造成的,我的程序(PHP)使用下面的语句是成功的,差异一是空格、二是大小写,供你参考:

header('Content-Disposition: attachment; filename="mysql.xls"');

⑤ java poi 在服务器生成excel文件

报格式错误是因为你没有填充EXCEL的内容。正确的做法是:1, HSSFWorkbook ws = new HSSFWorkbook();//建立新HSSFWorkbook对象2, Sheet sheet = workbook.createSheet(0); //建立一个新的sheet3,Row row = sheet.createRow(1); //建立一个新的row对象4, Cell cell = row.createCell(0); //在row上创建方格即列, cell.setCellValue(cellValue); //设置这个行中列的值 cell.setCellStyle(cellStyle); //设置样式

⑥ 如何使用Java POI生成Excel表文件

private static void WriteExcel2010() throws IOException { String path="C:\\poi2.xlsx"; XSSFWorkbook workbook=new XSSFWorkbook(); XSSFSheet sheet=workbook.createSheet("我的Sheet"); XSSFRow row=sheet.createRow(0); XSSFCell cell=row.createCell(0); cell.setCellValue("我是POI写入的"); XSSFRow row1=sheet.createRow(1); XSSFCell cell1=row1.createCell(0); cell1.setCellValue("2010"); FileOutputStream outputStream=new FileOutputStream(path); workbook.write(outputStream); outputStream.close(); } private static void WriteExcel2003() throws IOException { String path="C:\\poi2.xls"; HSSFWorkbook workbook=new HSSFWorkbook(); HSSFSheet sheet=workbook.createSheet("我的Excel"); HSSFRow row=sheet.createRow(0); HSSFCell cell=row.createCell(0); cell.setCellValue("我是POI写入的"); FileOutputStream outputStream=new FileOutputStream(path); workbook.write(outputStream); outputStream.close(); }

⑦ POI导出excel表时文件名变成乱码怎么办

在用POI进行excel表导出时,遇到中文文件名乱码问题,用下面的方法得到了解决。转载自:https://my.oschina.NET/chinamummy29/blog/525639在导出前对名称根据浏览器做下处理[java] view plain <code class="hljs typescript" style=""><span class="hljs-comment" style="">// 判断浏览器类型,firefox浏览器做特殊处理,否则下载文件名乱码</span> <span class="hljs-keyword" style="">public</span> <span class="hljs-keyword" style="">static</span> <span class="hljs-built_in" style="">void</span> compatibleFileName(HttpServletRequest request, HttpServletResponse response, <span class="hljs-built_in" style="">String</span> excelname) throws UnsupportedEncodingException { <span class="hljs-built_in" style="">String</span> agent = request.getHeader(<span class="hljs-string" style="">"USER-AGENT"</span>).toLowerCase(); response.setContentType(<span class="hljs-string" style="">"application/vnd.ms-excel"</span>); <span class="hljs-built_in" style="">String</span> fileName = excelname; <span class="hljs-built_in" style="">String</span> codedFileName = java.net.URLEncoder.encode(fileName, <span class="hljs-string" style="">"UTF-8"</span>); <span class="hljs-keyword" style="">if</span> (agent.contains(<span class="hljs-string" style="">"firefox"</span>)) { response.setCharacterEncoding(<span class="hljs-string" style="">"utf-8"</span>); response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + <span class="hljs-keyword" style="">new</span> <span class="hljs-built_in" style="">String</span>(fileName.getBytes(), <span class="hljs-string" style="">"ISO8859-1"</span>) + <span class="hljs-string" style="">".xls"</span>); } <span class="hljs-keyword" style="">else</span> { response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + codedFileName + <span class="hljs-string" style="">".xls"</span>); } }</code>

⑧ java poi导出excel

用spire.xls.jar也可以导出excel,代码更简单

import com.spire.xls.ExcelVersion;

import com.spire.xls.Workbook;

import com.spire.xls.Worksheet;

public class InsertArray {

public static void main(String[] args) {

//创建Workbook对象

Workbook wb = new Workbook();

//获取第一张工作表

Worksheet sheet = wb.getWorksheets().get(0);

//定义一维数据

String[] oneDimensionalArray = new String[]{"苹果", "梨子", "葡萄", "香蕉"};

//将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入

sheet.insertArray(oneDimensionalArray, 1, 1, true);

//定义二维数组

String[][] twoDimensionalArray = new String[][]{

{"姓名", "年龄", "性别", "学历"},

{"小张", "25", "男", "本科"},

{"小王", "24", "男", "本科"},

{"小李", "26", "女", "本科"}

};

//从指定单元格开始写入二维数组到工作表

sheet.insertArray(twoDimensionalArray, 1, 3);

//保存文档

wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);

}

}

⑨ jsp如何利用POI直接生成Excel并在页面中导出

java中导出Excel有两个组件可以使用,一个是jxl,一个是POI,我这里用的是POI。导出是可以在服务器上生成文件,然后下载,也可以利用输出流直接在网页中弹出对话框提示用户保存或下载。生成文件的方式会导致服务器中存在着垃圾文件,实现方式不太优雅,所以这里我采用的是后面直接通过输出流的方式。1、修改WEB服务器的CONF/web.xml,添加 <mime-mapping> <extension>xls</extension> <mime-type>application/vnd.ms-excel</mime-type> </mime-mapping> 如果不添加这个,那么在网页中下载的时候就变成了JSP文件2、download.jsp文件<%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*,com.shangyu.action.WriteExcel" pageEncoding="GBK"%><%response.setHeader("Content-Disposition","attachment;filename=test123.xls");//指定下载的文件名response.setContentType("application/vnd.ms-excel"); WriteExcel we=new WriteExcel();we.getExcel("111.xls",response.getOutputStream());%>注意不要有html代码,并且除了<% %> 中间的代码,其它的地方不要有空格。否则在导出文件的时候会在后台出现异常,虽然不影响程序的使用,到时令人看起来不太舒服3、WriteExcel.java 生成Excel的JavaBean,复杂的应用请查看APIpackage com.shangyu.action;import java.io.*;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;public class WriteExcel { public void getExcel(String sheetName,OutputStream output) { HSSFWorkbook wb=new HSSFWorkbook(); HSSFSheet sheet1=wb.createSheet("sheet1"); HSSFRow row=sheet1.createRow((short)0); HSSFCell cell=row.createCell((short)0); cell.setCellValue(1); row.createCell((short)1).setCellValue(2); row.createCell((short)2).setCellValue(3); row.createCell((short)3).setCellValue("中文字符");row=sheet1.createRow((short)1); cell=row.createCell((short)0); cell.setCellValue(1); row.createCell((short)1).setCellValue(2); row.createCell((short)2).setCellValue(3); row.createCell((short)3).setCellValue("中文字符"); //FileOutputStream fileout=new FileOutputStream("workbook.xls"); try { output.flush(); wb.write(output); output.close(); } catch (IOException e) { e.printStackTrace(); System.out.println( "Output is closed "); } }}通过以上三步,应该可以直接生成Excel文件下载或保存了,这在一些信息系统中相当有用。本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shangyu79/archive/2008/07/21/2682541.aspx

⑩ java web poi如何按查询结果导出相应的Excel

package com.aerolink.aocs.util.fileUtil;import java.io.FileOutputStream;import java.io.IOException;import java.util.Calendar;import java.util.Date;//import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFCellStyle;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class WriteExcelNew { /** * Excel文件 */ private XSSFWorkbook wb = null; /** * 输出Excel文件中的表对象 */ private XSSFSheet sheet = null; /** * 输出文件流 */ private FileOutputStream fileOut = null; /** * 输出文件名用户自定义 */ private String outputFilename = null; /** * 单元格样式 */ private XSSFCellStyle cellStyle = null; // private String newsheet = null; //输出Excel文件中的表名用户自定义 /** * 行 */ private XSSFRow row=null; /** * */ private int rowNumber=-1; /** * @param outputFilename * @param newsheet */ public WriteExcelNew(String outputFilename, String newsheet) { wb = new XSSFWorkbook(); //wb.setSheetName(1, "qwe");//设置第一张表的名称 sheet = wb.createSheet(newsheet); //sheet.setColumnWidth(1, 40);//第一行 列宽 this.outputFilename = outputFilename; // this.newsheet = newsheet; } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,int value)方法: * </p> * <p> * 将int数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, int value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } //row.setHeight((short)50);//行高 XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); } /** * <p> * Description:exportToExcelFile(short rownum,short cellnum,String value)方法: * </p> * <p> * 将String数据写入Execl文件的表中 * </p> * * @param rownum * @param cellnum * @param value */ public void exportToExcelFile(int rownum, int cellnum, String value) { if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格 rowNumber=rownum; row = sheet.createRow(rownum); }else if(rowNumber!=rownum){ rowNumber=rownum; row = sheet.createRow(rownum); } XSSFCell cell = row.createCell(cellnum); cell.setCellValue(value); if(cellStyle==null){ setCellStyle("center","center","",false); } cell.setCellStyle(cellStyle); }


赞 (0)