方法一:
可以先按照html的规则生成,然后通过stringBuffer相关截取.html和添加.xls
jxl poi开源的两个操作excel的三方组建
csv速度比较快,纯文本,但是对数据的内容有要求
个人新发现用html 的<table><tr><td> 标签也可以转换成excel
举例 a.html
<table>
<tr><td>1</td><td>1</td></tr>
<tr><td>2,000</td><td>2,000</td></tr>
<tr><td>3.1415926</td><td>3.1415926</td></tr>
</table>
再把文件名转换成a.xls就OK了
方法二:
打开excel文件的web.xml配置.txt
<mime-mapping>
<extension>xls</extension>
<mime-type>application/msexcel</mime-type>
</mime-mapping>
FileExportAction.java
===============================================================================================
package com.anyi.erp.oa.action;
import com.anyi.gp.context.ApplusContext;
import com.anyi.gp.core.dao.BaseDao;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.xwork.ActionSupport;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.Date;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.PageOrientation;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class FileExportAction extends ActionSupport
implements ServletRequestAware, ServletResponseAware
{
private static final long serialVersionUID = 4498487628145443321L;
private HttpServletResponse response;
private HttpServletRequest request;
private String fieldsName = "";
private String sqlId = "";
private String OA_TITLE = "";
private String parms="";
private String fieldsId="";
private String width="";
private String height="";
private JSONObject json=null;
private BaseDao baseDao;
String[] headersList;
private static final String MAIN_PATH= ApplusContext.getEnvironmentConfig().get("oaReportPath");
public void setFieldsName(String fieldsName) {
this.fieldsName = fieldsName;
}
public void setSqlId(String sqlId) {
this.sqlId = sqlId;
}
public void setTitles(String OA_TITLE) {
this.OA_TITLE = OA_TITLE;
}
public String execute() {
parms = request.getParameter("parms");
json = JSONObject.fromObject(parms);
OA_TITLE = request.getParameter("OA_TITLE");
fieldsName = request.getParameter("fieldsName");
fieldsId = request.getParameter("fieldsId");
width = request.getParameter("width");
height = request.getParameter("height");
System.out.println("width============="+width);
System.out.println("height============="+height);
System.out.println("OA_TITLE>>>>>>>>"+OA_TITLE);
System.out.println("fieldsName>>>>>>>>"+fieldsName);
System.out.println("fieldsId>>>>>>>>"+fieldsId);
System.out.println("MAIN_PATH==========="+MAIN_PATH);
System.out.println("json============="+json.toString());
WritableWorkbook wwb = null;
FileOutputStream os = null;
try
{
this.response.setContentType("text/xml; charset=GBK");
long time = System.currentTimeMillis();
String url = this.request.getRealPath("/");
System.out.println("URL=================="+url);
os = new FileOutputStream(url+"\\oaReport\\"+OA_TITLE+time+".xls");
wwb = Workbook.createWorkbook(os);
WritableSheet wsheet = wwb.createSheet("tb_1", 0);
wsheet.getSettings().setOrientation(
PageOrientation.LANDSCAPE);
this.headersList = this.fieldsId.split(",");
fillHeaders(wsheet, this.headersList);
fillContent(wsheet);
wwb.write();
os.flush();
// String url = this.request.getRequestURL().toString();
// url = url.substring(0,url.indexOf("OA"));
this.response.getWriter().print(OA_TITLE+time+".xls");
System.out.println("MANE==============="+OA_TITLE+time+".xls");
System.out.println("已返回Excel");
}
catch (Exception e) {
System.out.println("生成信息表(Excel格式)时出错:");
e.printStackTrace();
}finally{
try {
wwb.close();
os.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return MAIN_PATH+"\\"+OA_TITLE+System.currentTimeMillis()+".xls";
}
public void fillHeaders(WritableSheet wsheet, String[] headers)
throws RowsExceededException, WriteException
{
String[] widthargs = width.split(",");
for (int i = 0; i < this.headersList.length; ++i) {
wsheet.setColumnView(i, Long.valueOf(widthargs[i]).intValue());
}
wsheet.mergeCells(0, 0, headers.length - 1, 0);
WritableCellFormat format2 = getFormat(new WritableFont(
WritableFont.TIMES, 30, WritableFont.BOLD));
format2.setAlignment(Alignment.CENTRE);
Label label1 = new Label(0, 0, this.OA_TITLE , format2);
WritableCellFormat format1 = getFormat(new WritableFont(
WritableFont.TIMES, 15, WritableFont.BOLD));
format1.setAlignment(Alignment.CENTRE);
wsheet.addCell(label1);
for (int i = 0; i < headers.length; ++i) {
Label label = new Label(i, 1, headers[i], format1);
wsheet.addCell(label);
}
}
public static WritableCellFormat getFormat(WritableFont font)
throws RowsExceededException, WriteException
{
WritableCellFormat format1 = new WritableCellFormat(font);
format1.setVerticalAlignment(VerticalAlignment.CENTRE);
format1.setWrap(true);
format1.setBorder(Border.ALL, BorderLineStyle.THIN);
return format1;
}
public void fillContent(WritableSheet wsheet)
throws RowsExceededException, WriteException, SQLException
{
sqlId = request.getParameter("sqlid");
System.out.println("SQLID>>>>>>>"+sqlId);
List list = baseDao.queryForList(sqlId,json);
System.out.println(list);
String value="";
String[] field = fieldsName.split(",");
for (int i=0;i< list.size(); i++) {
// wsheet.setRowView(i+2, Long.valueOf(height).intValue());
for(int j=0;j<this.headersList.length;j++){
HashMap map =(HashMap)list.get(i);
if(map.get(field[j]) instanceof java.sql.Date) {
java.sql.Dated = (java.sql.Date) map.get(field[j]);
String d2 = new java.util.Date(d.getTime()).toLocaleString();
value =d2.substring(0, d2.indexOf("0:00:00"));
}else{
value = (String)map.get(field[j]);
}
Label label = new Label(j, i+2, value);
wsheet.addCell(label);
}
}
}
public void init() throws ServletException
{
}
public void setServletRequest(HttpServletRequest arg0)
{
this.request = arg0;
}
public void setServletResponse(HttpServletResponse arg0)
{
this.response = arg0;
}
public BaseDao getBaseDao() {
return baseDao;
}
public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}
}
分享到:
相关推荐
自定义的一个excel文件导入导出类,支持Excel2003到2010的格式。里面也有简单的配置文件的创建。
java导出excel超过65536条记录出错 java导出excel超过65536条记录出错 java导出excel超过65536条记录出错 java导出excel超过65536条记录出错
java导出Excel文件代码:从导入依赖、yml文件配置、前台vue、js代码、controller、service实现都有了
java导出excel文件,入参方便,灵活,实用性高,方便开发者
Java导出Excel文件(97--2003) 含jar包,注释全,可直接运行。
2、java读取数据库数据,并导出为excel文件。 3、README.md中有详细的操作步骤示例。 使用说明: 1. 先使用postman导入:other/excel相关.postman_collection.json 2. 导入sql到数据库:other/excel.sql 3. idea导入...
java导出excel文件并且下载到客户端(包括解决中文乱码)
支持从数据库中查询到数据,以多级表头形式导入到Excel文件中。比如:一级表头:学号、姓名、各科成绩。二级表头:语文、数学、英语(二级表头在各科成绩下面)。
java导出CVS,csv只是一种文件的形式。。。就是按照逗号(英文逗号)分隔的文件 它的做成跟普通的文件做成是一样一样的,只需要创建一个文件(*.csv),把字符串按照一定的格式(AAA,BBB\nCCC,DDD注意必须是英文的...
* 导出excel(利用反射机制) * String[] title = {"订单号","商品名称","商品价格","创建时间"}; * String[] field = {"orderNum","productName","productPrice","createTime"}; * ExportTools.export("sellJoin...
java导出内容以及图片到execl中,由于客户的骚需求,不得不进行一些不情愿的开发。文档仅供思路参考。谢谢
导出Excel文件利用apache的poi第三方包,可在阿帕奇官网下载(最新版本为3.8,支持office2010)。解压文件后,只需要把项目导入到eclipse,再将hibernate.cfg.xml里连接Oracle的URL和用户名、密码更改下。参考entity...
这个 rar 包中包含有一个导出Excel文件的类的源码 和 导出时要用到的 Jar包,可导出数据和图片到Excel文件中
需要使用JAVA 做EXCEL导出的同学,有问题尽管私信
Java导出数据到Excel文件中,支持多页签形式,如通过Java导出一个名为“各部门人员列表”,然后在文件中有三个页签,分别为“研发部”、“综合部”、“财务部”。其中这三个页签里面的数据就是通过Java导出到Excel...
struts2导出excel java 导出excel: JXL_excel: 可以直接在elipse下运行的例子 可以直接导出excel文件(包含所需的jar包) CSharpJExcel: 是JXL的官方下载的 包括详细的文档 和jar包
java开发中的列表数据导出成excel格式,这个文档包含了所需的jar包和例子代码。
使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用
在java中导出Excel文件,以及相应的jar包
该资源是一个rar压缩包形式的源代码,主要演示了java如何导出数据到Excle并保存到本地电脑、以及在web中如何导出数据库中的数据到Excel并下载。