麻烦给出详细的源码,插件贴出来。3Q。
10 个解决方案
#1
用POI 或者JXL 将你查询出的数据写入EXCEL
然后返回给浏览器EXCEL的服务器地址 浏览器会提示用户下载
用struts2 下载更简单
然后返回给浏览器EXCEL的服务器地址 浏览器会提示用户下载
用struts2 下载更简单
#2
我也想知道!!!up
#3
package actions;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import service.ICustomer;
import vo.Customer;
import jxl.Workbook;
import jxl.write.Border;
import jxl.write.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.opensymphony.xwork2.ActionSupport;
public class ReportInputOutputAction extends ActionSupport{
private String filePath="C:/Documents and Settings/Administrator/桌面",fileName="导出.xls";
private List list;
private ICustomer icust;
public String exportExcel(){
try {
OutputStream os;
// 导出到excel
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
os = new FileOutputStream(filePath + fileName);
WritableWorkbook wbook;
wbook = Workbook.createWorkbook(os);
WritableSheet wsheet = wbook.createSheet(fileName, 0); // Sheet1名字
WritableCellFormat wformat = new WritableCellFormat();// 表样式
wformat.setBorder(Border.ALL, BorderLineStyle.THIN);
WritableFont fontTitle = new WritableFont(WritableFont.TIMES,
12, WritableFont.BOLD);
WritableCellFormat wformatTitle = new WritableCellFormat(
fontTitle);// 表标题样式
wformatTitle.setBorder(Border.ALL, BorderLineStyle.THIN);
// 添加元素
wsheet.addCell(new Label(0, 0, "客户类型", wformatTitle));
wsheet.addCell(new Label(1, 0, "行业类型", wformatTitle));
wsheet.addCell(new Label(2, 0, "省", wformatTitle));
wsheet.addCell(new Label(3, 0, "姓名", wformatTitle));
wsheet.addCell(new Label(4, 0, "性别", wformatTitle));
wsheet.addCell(new Label(5, 0, "学历", wformatTitle));
wsheet.addCell(new Label(6, 0, "民族", wformatTitle));
wsheet.addCell(new Label(7, 0, "单位", wformatTitle));
wsheet.addCell(new Label(8, 0, "办公电话", wformatTitle));
wsheet.addCell(new Label(9, 0, "手机", wformatTitle));
wsheet.addCell(new Label(10, 0, "职务", wformatTitle));
wsheet.addCell(new Label(11, 0, "网站/邮箱", wformatTitle));
wsheet.addCell(new Label(12, 0, "传真", wformatTitle));
wsheet.addCell(new Label(13, 0, "QQ/MSN", wformatTitle));
wsheet.addCell(new Label(14, 0, "地址", wformatTitle));
wsheet.addCell(new Label(15, 0, "邮编", wformatTitle));
wsheet.addCell(new Label(16, 0, "出生日期", wformatTitle));
wsheet.addCell(new Label(17, 0, "状态", wformatTitle));
wsheet.addCell(new Label(18, 0, "备注", wformatTitle));
// 设置列宽
for(int i=0;i<19;i++){//共19列
wsheet.setColumnView(i, 25);
}
int count = 1;
list=icust.findAll("Customer");
if(list!=null){
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Customer customer=(Customer) list.get(i);
// new Label(m,n,v,k)中m表示列,n表示行,v表示值,k表示单元格样式
wsheet.addCell(new Label(0,count,customer.getCatype().getCaName(),wformat));
wsheet.addCell(new Label(1, count,customer.getHytype().getCaName(), wformat));
wsheet.addCell(new Label(2, count,customer.getPro().getProName(),wformat));
wsheet.addCell(new Label(3, count,customer.getCustName(), wformat));
wsheet.addCell(new Label(4,count,customer.getCustSex(),wformat));
wsheet.addCell(new Label(5, count,customer.getCustDegree(), wformat));
wsheet.addCell(new Label(6, count,customer.getCustRace(),wformat));
wsheet.addCell(new Label(7, count,customer.getCustOrg(), wformat));
wsheet.addCell(new Label(8, count,customer.getCustPhone(), wformat));
wsheet.addCell(new Label(9, count,customer.getCustTel(),wformat));
wsheet.addCell(new Label(10, count,customer.getCustPosition(), wformat));
wsheet.addCell(new Label(11, count,customer.getCustEmail(), wformat));
wsheet.addCell(new Label(12, count,customer.getCustFax(),wformat));
wsheet.addCell(new Label(13, count,customer.getCustQqmsn(), wformat));
wsheet.addCell(new Label(14, count,customer.getCustAddr(), wformat));
wsheet.addCell(new Label(15, count,customer.getCustPostcode(),wformat));
wsheet.addCell(new Label(16, count,customer.getCustBirth(), wformat));
wsheet.addCell(new Label(17, count,customer.getCustStatus(), wformat));
wsheet.addCell(new Label(18, count,customer.getCustInfo(),wformat));
count++;
}
}
wbook.write();
wbook.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
// 获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
public InputStream getInputStream() throws Exception {
// 获得文件
File f1 = new File(filePath + fileName);
InputStream fis = new FileInputStream(f1);
byte[] content = new byte[fis.available()];
fis.read(content);
// 和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
return new ByteArrayInputStream(content);
}
// 对于配置中的 ${fileName}, 获得下载保存时的文件名
public String getFileName() {
try {
// 中文文件名也是需要转码为 ISO8859-1,否则乱码
return new String(fileName.getBytes(), "ISO8859-1");
} catch (Exception e) {
return e.getMessage();
}
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public ICustomer getIcust() {
return icust;
}
public void setIcust(ICustomer icust) {
this.icust = icust;
}
//////////////struts.xml中的配置
<action name="reportInputOutputAction"
class="actions.ReportInputOutputAction">
<result name="success">
showCustomer.jsp
</result>
</action>
<action name="reportInputOutputExcel"
class="actions.OrderAction"
method="exportExcel">
<result name="success" type="stream">
<param name="contentType">
application/msexcel
</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<!-- bufferSize是fileStream 构造函数的参数,是设置文件流缓存区的大小-->
<param name="bufferSize">4096</param>
</result>
</action>
}
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import service.ICustomer;
import vo.Customer;
import jxl.Workbook;
import jxl.write.Border;
import jxl.write.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.opensymphony.xwork2.ActionSupport;
public class ReportInputOutputAction extends ActionSupport{
private String filePath="C:/Documents and Settings/Administrator/桌面",fileName="导出.xls";
private List list;
private ICustomer icust;
public String exportExcel(){
try {
OutputStream os;
// 导出到excel
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
os = new FileOutputStream(filePath + fileName);
WritableWorkbook wbook;
wbook = Workbook.createWorkbook(os);
WritableSheet wsheet = wbook.createSheet(fileName, 0); // Sheet1名字
WritableCellFormat wformat = new WritableCellFormat();// 表样式
wformat.setBorder(Border.ALL, BorderLineStyle.THIN);
WritableFont fontTitle = new WritableFont(WritableFont.TIMES,
12, WritableFont.BOLD);
WritableCellFormat wformatTitle = new WritableCellFormat(
fontTitle);// 表标题样式
wformatTitle.setBorder(Border.ALL, BorderLineStyle.THIN);
// 添加元素
wsheet.addCell(new Label(0, 0, "客户类型", wformatTitle));
wsheet.addCell(new Label(1, 0, "行业类型", wformatTitle));
wsheet.addCell(new Label(2, 0, "省", wformatTitle));
wsheet.addCell(new Label(3, 0, "姓名", wformatTitle));
wsheet.addCell(new Label(4, 0, "性别", wformatTitle));
wsheet.addCell(new Label(5, 0, "学历", wformatTitle));
wsheet.addCell(new Label(6, 0, "民族", wformatTitle));
wsheet.addCell(new Label(7, 0, "单位", wformatTitle));
wsheet.addCell(new Label(8, 0, "办公电话", wformatTitle));
wsheet.addCell(new Label(9, 0, "手机", wformatTitle));
wsheet.addCell(new Label(10, 0, "职务", wformatTitle));
wsheet.addCell(new Label(11, 0, "网站/邮箱", wformatTitle));
wsheet.addCell(new Label(12, 0, "传真", wformatTitle));
wsheet.addCell(new Label(13, 0, "QQ/MSN", wformatTitle));
wsheet.addCell(new Label(14, 0, "地址", wformatTitle));
wsheet.addCell(new Label(15, 0, "邮编", wformatTitle));
wsheet.addCell(new Label(16, 0, "出生日期", wformatTitle));
wsheet.addCell(new Label(17, 0, "状态", wformatTitle));
wsheet.addCell(new Label(18, 0, "备注", wformatTitle));
// 设置列宽
for(int i=0;i<19;i++){//共19列
wsheet.setColumnView(i, 25);
}
int count = 1;
list=icust.findAll("Customer");
if(list!=null){
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Customer customer=(Customer) list.get(i);
// new Label(m,n,v,k)中m表示列,n表示行,v表示值,k表示单元格样式
wsheet.addCell(new Label(0,count,customer.getCatype().getCaName(),wformat));
wsheet.addCell(new Label(1, count,customer.getHytype().getCaName(), wformat));
wsheet.addCell(new Label(2, count,customer.getPro().getProName(),wformat));
wsheet.addCell(new Label(3, count,customer.getCustName(), wformat));
wsheet.addCell(new Label(4,count,customer.getCustSex(),wformat));
wsheet.addCell(new Label(5, count,customer.getCustDegree(), wformat));
wsheet.addCell(new Label(6, count,customer.getCustRace(),wformat));
wsheet.addCell(new Label(7, count,customer.getCustOrg(), wformat));
wsheet.addCell(new Label(8, count,customer.getCustPhone(), wformat));
wsheet.addCell(new Label(9, count,customer.getCustTel(),wformat));
wsheet.addCell(new Label(10, count,customer.getCustPosition(), wformat));
wsheet.addCell(new Label(11, count,customer.getCustEmail(), wformat));
wsheet.addCell(new Label(12, count,customer.getCustFax(),wformat));
wsheet.addCell(new Label(13, count,customer.getCustQqmsn(), wformat));
wsheet.addCell(new Label(14, count,customer.getCustAddr(), wformat));
wsheet.addCell(new Label(15, count,customer.getCustPostcode(),wformat));
wsheet.addCell(new Label(16, count,customer.getCustBirth(), wformat));
wsheet.addCell(new Label(17, count,customer.getCustStatus(), wformat));
wsheet.addCell(new Label(18, count,customer.getCustInfo(),wformat));
count++;
}
}
wbook.write();
wbook.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
// 获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
public InputStream getInputStream() throws Exception {
// 获得文件
File f1 = new File(filePath + fileName);
InputStream fis = new FileInputStream(f1);
byte[] content = new byte[fis.available()];
fis.read(content);
// 和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
return new ByteArrayInputStream(content);
}
// 对于配置中的 ${fileName}, 获得下载保存时的文件名
public String getFileName() {
try {
// 中文文件名也是需要转码为 ISO8859-1,否则乱码
return new String(fileName.getBytes(), "ISO8859-1");
} catch (Exception e) {
return e.getMessage();
}
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public ICustomer getIcust() {
return icust;
}
public void setIcust(ICustomer icust) {
this.icust = icust;
}
//////////////struts.xml中的配置
<action name="reportInputOutputAction"
class="actions.ReportInputOutputAction">
<result name="success">
showCustomer.jsp
</result>
</action>
<action name="reportInputOutputExcel"
class="actions.OrderAction"
method="exportExcel">
<result name="success" type="stream">
<param name="contentType">
application/msexcel
</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<!-- bufferSize是fileStream 构造函数的参数,是设置文件流缓存区的大小-->
<param name="bufferSize">4096</param>
</result>
</action>
}
#4
对了要先导入jxl.jar这个包
#5
补充一下,保存的路径是用户选择的.
继续等ing。。。
继续等ing。。。
#6
用displaytag标签,
需要导入2个jar包 displaytag-1.1.1.jar / displaytag-export-poi-1.1.1.jar就行
用法如下:
id随便定义,name是你的action绑定数据值
这个标签不仅能解决自动分页 也能解决你说的导出功能,而且可以有3种格式:
xml/excel/cvs。
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>
<display:table id="user" export="true" class="mars" name="sessionScope.users" pagesize="15" >
<display:column property="id" title="编号" sortable="true"/>
<display:column property="userName" title="姓名" sortable="true" />
<display:column property="birthday" title="出生日期" sortable="true"/>
<display:column property="email" title="邮箱地址" sortable="true"/>
</display:table>
需要导入2个jar包 displaytag-1.1.1.jar / displaytag-export-poi-1.1.1.jar就行
用法如下:
id随便定义,name是你的action绑定数据值
这个标签不仅能解决自动分页 也能解决你说的导出功能,而且可以有3种格式:
xml/excel/cvs。
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>
<display:table id="user" export="true" class="mars" name="sessionScope.users" pagesize="15" >
<display:column property="id" title="编号" sortable="true"/>
<display:column property="userName" title="姓名" sortable="true" />
<display:column property="birthday" title="出生日期" sortable="true"/>
<display:column property="email" title="邮箱地址" sortable="true"/>
</display:table>
#7
<% page contentType="Application/vnd.ms-excel;charset=gbk"%>
<%
response.setContentType("Application/vnd.ms-excel;charset=Gbk");
response.setHeader("Content-Disposition","attachment;filename=cd.xls")
%>
新建一个JSP(copy数据页面,按上面改下JSP文件头即可),点击导出按钮跳转至该页面,
<%
response.setContentType("Application/vnd.ms-excel;charset=Gbk");
response.setHeader("Content-Disposition","attachment;filename=cd.xls")
%>
新建一个JSP(copy数据页面,按上面改下JSP文件头即可),点击导出按钮跳转至该页面,
#8
代码就不说了,都有!
选择路径就是:
workbook为POI包中的HSSFWorkbook这个类!
选择路径就是:
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/msexcel;charset=UTF-8");
workbook.write(out);
out.close();
workbook为POI包中的HSSFWorkbook这个类!
#9
拜托,那你把路径path改一下不就得了
即使我的路径是保存在桌面上的但用户任可以自己选择地点!
#10
我现在接触的有两种,一个是通过jxl.jar的包,还有就是通过poi的。但鉴于这些代码都不是我写的,所以就不贴出来了。如果那些数据你都查出来了,你肯定是放在一个对象里面,map或者是list里面或其他,把这个循环写进excel不就可以了。我们现在用到的是poi,使用的有:
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;
楼主可以下个jxl.jar的包,用楼上的方法试试。在改成你自己需要的。gl
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;
楼主可以下个jxl.jar的包,用楼上的方法试试。在改成你自己需要的。gl
#1
用POI 或者JXL 将你查询出的数据写入EXCEL
然后返回给浏览器EXCEL的服务器地址 浏览器会提示用户下载
用struts2 下载更简单
然后返回给浏览器EXCEL的服务器地址 浏览器会提示用户下载
用struts2 下载更简单
#2
我也想知道!!!up
#3
package actions;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import service.ICustomer;
import vo.Customer;
import jxl.Workbook;
import jxl.write.Border;
import jxl.write.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.opensymphony.xwork2.ActionSupport;
public class ReportInputOutputAction extends ActionSupport{
private String filePath="C:/Documents and Settings/Administrator/桌面",fileName="导出.xls";
private List list;
private ICustomer icust;
public String exportExcel(){
try {
OutputStream os;
// 导出到excel
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
os = new FileOutputStream(filePath + fileName);
WritableWorkbook wbook;
wbook = Workbook.createWorkbook(os);
WritableSheet wsheet = wbook.createSheet(fileName, 0); // Sheet1名字
WritableCellFormat wformat = new WritableCellFormat();// 表样式
wformat.setBorder(Border.ALL, BorderLineStyle.THIN);
WritableFont fontTitle = new WritableFont(WritableFont.TIMES,
12, WritableFont.BOLD);
WritableCellFormat wformatTitle = new WritableCellFormat(
fontTitle);// 表标题样式
wformatTitle.setBorder(Border.ALL, BorderLineStyle.THIN);
// 添加元素
wsheet.addCell(new Label(0, 0, "客户类型", wformatTitle));
wsheet.addCell(new Label(1, 0, "行业类型", wformatTitle));
wsheet.addCell(new Label(2, 0, "省", wformatTitle));
wsheet.addCell(new Label(3, 0, "姓名", wformatTitle));
wsheet.addCell(new Label(4, 0, "性别", wformatTitle));
wsheet.addCell(new Label(5, 0, "学历", wformatTitle));
wsheet.addCell(new Label(6, 0, "民族", wformatTitle));
wsheet.addCell(new Label(7, 0, "单位", wformatTitle));
wsheet.addCell(new Label(8, 0, "办公电话", wformatTitle));
wsheet.addCell(new Label(9, 0, "手机", wformatTitle));
wsheet.addCell(new Label(10, 0, "职务", wformatTitle));
wsheet.addCell(new Label(11, 0, "网站/邮箱", wformatTitle));
wsheet.addCell(new Label(12, 0, "传真", wformatTitle));
wsheet.addCell(new Label(13, 0, "QQ/MSN", wformatTitle));
wsheet.addCell(new Label(14, 0, "地址", wformatTitle));
wsheet.addCell(new Label(15, 0, "邮编", wformatTitle));
wsheet.addCell(new Label(16, 0, "出生日期", wformatTitle));
wsheet.addCell(new Label(17, 0, "状态", wformatTitle));
wsheet.addCell(new Label(18, 0, "备注", wformatTitle));
// 设置列宽
for(int i=0;i<19;i++){//共19列
wsheet.setColumnView(i, 25);
}
int count = 1;
list=icust.findAll("Customer");
if(list!=null){
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Customer customer=(Customer) list.get(i);
// new Label(m,n,v,k)中m表示列,n表示行,v表示值,k表示单元格样式
wsheet.addCell(new Label(0,count,customer.getCatype().getCaName(),wformat));
wsheet.addCell(new Label(1, count,customer.getHytype().getCaName(), wformat));
wsheet.addCell(new Label(2, count,customer.getPro().getProName(),wformat));
wsheet.addCell(new Label(3, count,customer.getCustName(), wformat));
wsheet.addCell(new Label(4,count,customer.getCustSex(),wformat));
wsheet.addCell(new Label(5, count,customer.getCustDegree(), wformat));
wsheet.addCell(new Label(6, count,customer.getCustRace(),wformat));
wsheet.addCell(new Label(7, count,customer.getCustOrg(), wformat));
wsheet.addCell(new Label(8, count,customer.getCustPhone(), wformat));
wsheet.addCell(new Label(9, count,customer.getCustTel(),wformat));
wsheet.addCell(new Label(10, count,customer.getCustPosition(), wformat));
wsheet.addCell(new Label(11, count,customer.getCustEmail(), wformat));
wsheet.addCell(new Label(12, count,customer.getCustFax(),wformat));
wsheet.addCell(new Label(13, count,customer.getCustQqmsn(), wformat));
wsheet.addCell(new Label(14, count,customer.getCustAddr(), wformat));
wsheet.addCell(new Label(15, count,customer.getCustPostcode(),wformat));
wsheet.addCell(new Label(16, count,customer.getCustBirth(), wformat));
wsheet.addCell(new Label(17, count,customer.getCustStatus(), wformat));
wsheet.addCell(new Label(18, count,customer.getCustInfo(),wformat));
count++;
}
}
wbook.write();
wbook.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
// 获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
public InputStream getInputStream() throws Exception {
// 获得文件
File f1 = new File(filePath + fileName);
InputStream fis = new FileInputStream(f1);
byte[] content = new byte[fis.available()];
fis.read(content);
// 和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
return new ByteArrayInputStream(content);
}
// 对于配置中的 ${fileName}, 获得下载保存时的文件名
public String getFileName() {
try {
// 中文文件名也是需要转码为 ISO8859-1,否则乱码
return new String(fileName.getBytes(), "ISO8859-1");
} catch (Exception e) {
return e.getMessage();
}
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public ICustomer getIcust() {
return icust;
}
public void setIcust(ICustomer icust) {
this.icust = icust;
}
//////////////struts.xml中的配置
<action name="reportInputOutputAction"
class="actions.ReportInputOutputAction">
<result name="success">
showCustomer.jsp
</result>
</action>
<action name="reportInputOutputExcel"
class="actions.OrderAction"
method="exportExcel">
<result name="success" type="stream">
<param name="contentType">
application/msexcel
</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<!-- bufferSize是fileStream 构造函数的参数,是设置文件流缓存区的大小-->
<param name="bufferSize">4096</param>
</result>
</action>
}
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import service.ICustomer;
import vo.Customer;
import jxl.Workbook;
import jxl.write.Border;
import jxl.write.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.opensymphony.xwork2.ActionSupport;
public class ReportInputOutputAction extends ActionSupport{
private String filePath="C:/Documents and Settings/Administrator/桌面",fileName="导出.xls";
private List list;
private ICustomer icust;
public String exportExcel(){
try {
OutputStream os;
// 导出到excel
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
os = new FileOutputStream(filePath + fileName);
WritableWorkbook wbook;
wbook = Workbook.createWorkbook(os);
WritableSheet wsheet = wbook.createSheet(fileName, 0); // Sheet1名字
WritableCellFormat wformat = new WritableCellFormat();// 表样式
wformat.setBorder(Border.ALL, BorderLineStyle.THIN);
WritableFont fontTitle = new WritableFont(WritableFont.TIMES,
12, WritableFont.BOLD);
WritableCellFormat wformatTitle = new WritableCellFormat(
fontTitle);// 表标题样式
wformatTitle.setBorder(Border.ALL, BorderLineStyle.THIN);
// 添加元素
wsheet.addCell(new Label(0, 0, "客户类型", wformatTitle));
wsheet.addCell(new Label(1, 0, "行业类型", wformatTitle));
wsheet.addCell(new Label(2, 0, "省", wformatTitle));
wsheet.addCell(new Label(3, 0, "姓名", wformatTitle));
wsheet.addCell(new Label(4, 0, "性别", wformatTitle));
wsheet.addCell(new Label(5, 0, "学历", wformatTitle));
wsheet.addCell(new Label(6, 0, "民族", wformatTitle));
wsheet.addCell(new Label(7, 0, "单位", wformatTitle));
wsheet.addCell(new Label(8, 0, "办公电话", wformatTitle));
wsheet.addCell(new Label(9, 0, "手机", wformatTitle));
wsheet.addCell(new Label(10, 0, "职务", wformatTitle));
wsheet.addCell(new Label(11, 0, "网站/邮箱", wformatTitle));
wsheet.addCell(new Label(12, 0, "传真", wformatTitle));
wsheet.addCell(new Label(13, 0, "QQ/MSN", wformatTitle));
wsheet.addCell(new Label(14, 0, "地址", wformatTitle));
wsheet.addCell(new Label(15, 0, "邮编", wformatTitle));
wsheet.addCell(new Label(16, 0, "出生日期", wformatTitle));
wsheet.addCell(new Label(17, 0, "状态", wformatTitle));
wsheet.addCell(new Label(18, 0, "备注", wformatTitle));
// 设置列宽
for(int i=0;i<19;i++){//共19列
wsheet.setColumnView(i, 25);
}
int count = 1;
list=icust.findAll("Customer");
if(list!=null){
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Customer customer=(Customer) list.get(i);
// new Label(m,n,v,k)中m表示列,n表示行,v表示值,k表示单元格样式
wsheet.addCell(new Label(0,count,customer.getCatype().getCaName(),wformat));
wsheet.addCell(new Label(1, count,customer.getHytype().getCaName(), wformat));
wsheet.addCell(new Label(2, count,customer.getPro().getProName(),wformat));
wsheet.addCell(new Label(3, count,customer.getCustName(), wformat));
wsheet.addCell(new Label(4,count,customer.getCustSex(),wformat));
wsheet.addCell(new Label(5, count,customer.getCustDegree(), wformat));
wsheet.addCell(new Label(6, count,customer.getCustRace(),wformat));
wsheet.addCell(new Label(7, count,customer.getCustOrg(), wformat));
wsheet.addCell(new Label(8, count,customer.getCustPhone(), wformat));
wsheet.addCell(new Label(9, count,customer.getCustTel(),wformat));
wsheet.addCell(new Label(10, count,customer.getCustPosition(), wformat));
wsheet.addCell(new Label(11, count,customer.getCustEmail(), wformat));
wsheet.addCell(new Label(12, count,customer.getCustFax(),wformat));
wsheet.addCell(new Label(13, count,customer.getCustQqmsn(), wformat));
wsheet.addCell(new Label(14, count,customer.getCustAddr(), wformat));
wsheet.addCell(new Label(15, count,customer.getCustPostcode(),wformat));
wsheet.addCell(new Label(16, count,customer.getCustBirth(), wformat));
wsheet.addCell(new Label(17, count,customer.getCustStatus(), wformat));
wsheet.addCell(new Label(18, count,customer.getCustInfo(),wformat));
count++;
}
}
wbook.write();
wbook.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
// 获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
public InputStream getInputStream() throws Exception {
// 获得文件
File f1 = new File(filePath + fileName);
InputStream fis = new FileInputStream(f1);
byte[] content = new byte[fis.available()];
fis.read(content);
// 和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
return new ByteArrayInputStream(content);
}
// 对于配置中的 ${fileName}, 获得下载保存时的文件名
public String getFileName() {
try {
// 中文文件名也是需要转码为 ISO8859-1,否则乱码
return new String(fileName.getBytes(), "ISO8859-1");
} catch (Exception e) {
return e.getMessage();
}
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public ICustomer getIcust() {
return icust;
}
public void setIcust(ICustomer icust) {
this.icust = icust;
}
//////////////struts.xml中的配置
<action name="reportInputOutputAction"
class="actions.ReportInputOutputAction">
<result name="success">
showCustomer.jsp
</result>
</action>
<action name="reportInputOutputExcel"
class="actions.OrderAction"
method="exportExcel">
<result name="success" type="stream">
<param name="contentType">
application/msexcel
</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<!-- bufferSize是fileStream 构造函数的参数,是设置文件流缓存区的大小-->
<param name="bufferSize">4096</param>
</result>
</action>
}
#4
对了要先导入jxl.jar这个包
#5
补充一下,保存的路径是用户选择的.
继续等ing。。。
继续等ing。。。
#6
用displaytag标签,
需要导入2个jar包 displaytag-1.1.1.jar / displaytag-export-poi-1.1.1.jar就行
用法如下:
id随便定义,name是你的action绑定数据值
这个标签不仅能解决自动分页 也能解决你说的导出功能,而且可以有3种格式:
xml/excel/cvs。
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>
<display:table id="user" export="true" class="mars" name="sessionScope.users" pagesize="15" >
<display:column property="id" title="编号" sortable="true"/>
<display:column property="userName" title="姓名" sortable="true" />
<display:column property="birthday" title="出生日期" sortable="true"/>
<display:column property="email" title="邮箱地址" sortable="true"/>
</display:table>
需要导入2个jar包 displaytag-1.1.1.jar / displaytag-export-poi-1.1.1.jar就行
用法如下:
id随便定义,name是你的action绑定数据值
这个标签不仅能解决自动分页 也能解决你说的导出功能,而且可以有3种格式:
xml/excel/cvs。
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>
<display:table id="user" export="true" class="mars" name="sessionScope.users" pagesize="15" >
<display:column property="id" title="编号" sortable="true"/>
<display:column property="userName" title="姓名" sortable="true" />
<display:column property="birthday" title="出生日期" sortable="true"/>
<display:column property="email" title="邮箱地址" sortable="true"/>
</display:table>
#7
<% page contentType="Application/vnd.ms-excel;charset=gbk"%>
<%
response.setContentType("Application/vnd.ms-excel;charset=Gbk");
response.setHeader("Content-Disposition","attachment;filename=cd.xls")
%>
新建一个JSP(copy数据页面,按上面改下JSP文件头即可),点击导出按钮跳转至该页面,
<%
response.setContentType("Application/vnd.ms-excel;charset=Gbk");
response.setHeader("Content-Disposition","attachment;filename=cd.xls")
%>
新建一个JSP(copy数据页面,按上面改下JSP文件头即可),点击导出按钮跳转至该页面,
#8
代码就不说了,都有!
选择路径就是:
workbook为POI包中的HSSFWorkbook这个类!
选择路径就是:
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/msexcel;charset=UTF-8");
workbook.write(out);
out.close();
workbook为POI包中的HSSFWorkbook这个类!
#9
拜托,那你把路径path改一下不就得了
即使我的路径是保存在桌面上的但用户任可以自己选择地点!
#10
我现在接触的有两种,一个是通过jxl.jar的包,还有就是通过poi的。但鉴于这些代码都不是我写的,所以就不贴出来了。如果那些数据你都查出来了,你肯定是放在一个对象里面,map或者是list里面或其他,把这个循环写进excel不就可以了。我们现在用到的是poi,使用的有:
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;
楼主可以下个jxl.jar的包,用楼上的方法试试。在改成你自己需要的。gl
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;
楼主可以下个jxl.jar的包,用楼上的方法试试。在改成你自己需要的。gl