一个关于文件、数据导出的问题 在web项目中, 实现文件、数据的导出, 将数据库中的数据导出到电脑上。用的jsp。要求,在一个列表中,可以选择你想要导出的部分导出,也可以全部导出。 还可以自定义保存路径Path、文件名、文件格式(txt、doc、excel) 。 如何实现? 新人 , 求助。 请大大们帮帮忙, 谢谢! 万分感谢。! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 导出报表 //界面<button onclick="exportss()" class="common_button" type="button">导出报表</button>function exportss() {if(idexport==""){ alert("没有数据值可导出"); return false;} window.location.href = '<eaglelink:contextpath/>/html/messagestat/messagestat!out.action?idexport='+idexport; }//后台 public String out() { String ids = request.getParameter("idexport"); String para = ids.substring(0, ids.length() - 1); Vector<Param> params = new Vector<Param>(); if (ids != null) { Param param = new Param(); param.setParamName("messageId"); param.setOperator("in"); param.setParamValue(para); params.add(param); } List<MessageStat> msgList = (List<MessageStat>) service.getCollection("MessageStat", params, null); if (msgList != null && msgList.size() > 0) { String[][] str = new String[msgList.size() + 1][8]; str[0][0] = "序号"; str[0][1] = "名称"; str[0][2] = "日期"; str[0][3] = "开始时间"; str[0][4] = "结束时间"; str[0][5] = "发送量"; str[0][6] = "接收量"; str[0][7] = "浏览量"; for (int i = 0; i < msgList.size(); i++) { logger.debug(msgList.get(i).getMessageId()); MessageStat messagestat = msgList.get(i); str[i + 1][0] = "" + i + 1; str[i + 1][1] = messagestat.getMessageSubject(); str[i + 1][2] = DateFormater.formatDateChinese(messagestat.getStatTime()) + ""; str[i + 1][3] = DateFormater.formatDateChinese(messagestat.getStatTime()) + ""; str[i + 1][4] = DateFormater.formatDateChinese(messagestat.getOverTime()) + ""; str[i + 1][5] = messagestat.getMessageTotalNums() + ""; str[i + 1][6] = messagestat.getMessageReceiveNums() + ""; str[i + 1][7] = messagestat.getMessageReadNums() + ""; } try { String vieName = URLEncoder.encode("按日期数据统计.xls", "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + vieName); String filepath = getWebApplicationAbsolutePath() + System.currentTimeMillis() + ".xls"; logger.debug("获取文件的路径为:" + filepath); ExcelJxl.getDateExcel(str, filepath, "按日期数据统计",7); InputStream is = null; try { is = new FileInputStream(filepath); } catch (FileNotFoundException e) { logger.error("导出的文件没有找到", e); } byte[] bytes = null; try { bytes = new byte[is.available()]; is.read(bytes); is.close(); } catch (IOException e) { e.printStackTrace(); } try { response.getOutputStream().write(bytes); response.setContentType("xls"); response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } File file = new File(filepath); if (file != null && file.isFile() && file.exists()) { boolean b = file.delete(); if (b) { logger.debug("删除临时文件成功"); } else { logger.warn("删除临时文件失败"); } } } catch (UnsupportedEncodingException e) { logger.error("转换按日期统计.xls为UTF-8编码失败", e); } }//支持类import java.io.FileOutputStream;import java.io.OutputStream;import org.apache.log4j.Logger;import jxl.Workbook;import jxl.format.BorderLineStyle;import jxl.format.VerticalAlignment;import jxl.write.Label;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class ExcelJxl { private static Logger logger = Logger.getLogger(ExcelJxl.class); /** * excel格式导出 * @param alldata 组织好的数据 * @param outFileStr 文件输出路径 * @param tableName 表头名称 * @param index 表头截止位置x轴坐标起始点为0 * */ @SuppressWarnings("deprecation") public static void getDateExcel(String[][] alldata,String outFileStr,String tableName,int index) { try { //创建小字体:Arial,大小为8号,非粗体,非斜体 WritableFont wf = new WritableFont(WritableFont.ARIAL, 8,WritableFont.NO_BOLD, false); //创建大字体:Arial,大小为18号,粗体,非斜体 WritableFont Bwf = new WritableFont(WritableFont.ARIAL, 14,WritableFont.NO_BOLD, false); //Bwf.setColour(jxl.format.Colour.RED); //创建单元格格式:设置水平对齐为向右对齐 jxl.write.WritableCellFormat RwcfF = new jxl.write.WritableCellFormat(wf); RwcfF.setAlignment(jxl.write.Alignment.RIGHT); //创建单元格格式:设置水平对齐为向左对齐 jxl.write.WritableCellFormat LwcfF = new jxl.write.WritableCellFormat(wf); LwcfF.setAlignment(jxl.write.Alignment.LEFT); //创建单元格格式:设置水平对齐为居中对齐 jxl.write.WritableCellFormat CwcfF = new jxl.write.WritableCellFormat(wf); CwcfF.setAlignment(jxl.write.Alignment.CENTRE); jxl.write.WritableCellFormat CBwcfF = new jxl.write.WritableCellFormat(Bwf); CBwcfF.setAlignment(jxl.write.Alignment.CENTRE); //设置垂直对齐为居中对齐 CBwcfF.setVerticalAlignment(VerticalAlignment.CENTRE); //设置顶部边框线为实线(默认是黑色--也可以设置其他颜色) CBwcfF.setBorder(jxl.format.Border.TOP, BorderLineStyle.MEDIUM); //设置右边框线为实线 CBwcfF.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.MEDIUM); //设置顶部框线为实线 CBwcfF.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.MEDIUM); jxl.write.WritableCellFormat CMwcfF = new jxl.write.WritableCellFormat(wf); CMwcfF.setAlignment(jxl.write.Alignment.LEFT); //设置垂直对齐为向上对齐 CMwcfF.setVerticalAlignment(VerticalAlignment.TOP); CMwcfF.setWrap(true); // File tFile = new File(templatePath);//创建模板文件对象 //创建文件输出流对象 //FileOutputStream os = new FileOutputStream(outFile); OutputStream os = new FileOutputStream(outFileStr); //模板工作簿对象 //输出工作簿对象 WritableWorkbook wbook = Workbook.createWorkbook(os); WritableSheet wsheet =wbook.createSheet("log", 0); wsheet.setColumnView(0, 10); // 设置列的宽度 wsheet.setColumnView(1, 10); // 设置列的宽度 wsheet.setColumnView(2, 10); // 设置列的宽度 wsheet.setColumnView(3, 10); // 设置列的宽度 wsheet.setColumnView(4, 10); // 设置列的宽度 wsheet.setColumnView(5, 10); // 设置列的宽度 wsheet.mergeCells(0, 0, index, 0); // 合并单元格 wsheet.addCell(new Label(0, 0,tableName, CBwcfF)); for (int i = 0; i < alldata.length; i++) { for (int j = 0; j < alldata[i].length; j++) { wsheet.addCell(new Label(j, i+1, alldata[i][j], LwcfF)); if(alldata[i][j]!=null) { if(alldata[i][j].length()>wsheet.getColumnWidth(j)) { wsheet.setColumnView(j, alldata[i][j].length()); // 设置列的宽度 } } } }// //在坐标为(0,0)的单元格写入"测试"字符串使用8号红色小字体,向右对齐// // //在坐标为(1,1)的单元格写入"test"字符串使用8号红色小字体,向左对齐// wsheet.addCell(new Label(1, 1, "test", LwcfF));// //在坐标为(2,2)的单元格写入"测试test"字符串使用8号红色小字体,居中对齐// wsheet.addCell(new Label(2,2, "测试test", CMwcfF)); //写入 wbook.write(); wbook.close(); //关闭文件输出流 os.close(); } catch(Exception e) { logger.error("创建excel jxl 异常",e); } } 楼主不要忘记导入相关jar包! 那个jar包是 jxl.jar 是不? 谢谢! 那个jar包, 就是jxl.jar吧。 excel的文件并没有一定说是xls结尾的吧,你可以保存成csv的。这样就不需要jxl 新手入门困惑不解啊 javascript的验证输入语句 这里的 contactDao.save(contact); 是什么意思? struts分页view中遇到了一个问题,急,在线等!! 人家ASP有UrlRewrite技术,JSP有吗?? 我的TOMCAT问题!? 请问在jsp中分页应该怎么做 用jspSmartUpload上传AutoCAD文件到数据库,下载下来出现乱码!!其他方式没乱码 关于MD5加密同一个字符串调用方式不同,返回密文不同。见鬼啦!!!!! 请问这样连接邮箱可以吗(使用javamail)? Tomcat绑定非80端口域名 讨论一下远程调用后台服务开发模式的可行性
<button onclick="exportss()" class="common_button" type="button">导出报表</button>function exportss() {
if(idexport=="")
{
alert("没有数据值可导出");
return false;
}
window.location.href = '<eaglelink:contextpath/>/html/messagestat/messagestat!out.action?idexport='+idexport;
}
//后台
public String out() {
String ids = request.getParameter("idexport");
String para = ids.substring(0, ids.length() - 1);
Vector<Param> params = new Vector<Param>();
if (ids != null) {
Param param = new Param();
param.setParamName("messageId");
param.setOperator("in");
param.setParamValue(para);
params.add(param);
}
List<MessageStat> msgList = (List<MessageStat>) service.getCollection("MessageStat", params, null);
if (msgList != null && msgList.size() > 0) {
String[][] str = new String[msgList.size() + 1][8];
str[0][0] = "序号";
str[0][1] = "名称";
str[0][2] = "日期";
str[0][3] = "开始时间";
str[0][4] = "结束时间";
str[0][5] = "发送量";
str[0][6] = "接收量";
str[0][7] = "浏览量";
for (int i = 0; i < msgList.size(); i++) {
logger.debug(msgList.get(i).getMessageId());
MessageStat messagestat = msgList.get(i);
str[i + 1][0] = "" + i + 1;
str[i + 1][1] = messagestat.getMessageSubject();
str[i + 1][2] = DateFormater.formatDateChinese(messagestat.getStatTime()) + "";
str[i + 1][3] = DateFormater.formatDateChinese(messagestat.getStatTime()) + "";
str[i + 1][4] = DateFormater.formatDateChinese(messagestat.getOverTime()) + "";
str[i + 1][5] = messagestat.getMessageTotalNums() + "";
str[i + 1][6] = messagestat.getMessageReceiveNums() + "";
str[i + 1][7] = messagestat.getMessageReadNums() + "";
}
try {
String vieName = URLEncoder.encode("按日期数据统计.xls", "UTF-8");
response.addHeader("Content-Disposition",
"attachment;filename=" + vieName);
String filepath = getWebApplicationAbsolutePath()
+ System.currentTimeMillis() + ".xls";
logger.debug("获取文件的路径为:" + filepath);
ExcelJxl.getDateExcel(str, filepath, "按日期数据统计",7);
InputStream is = null;
try {
is = new FileInputStream(filepath);
} catch (FileNotFoundException e) {
logger.error("导出的文件没有找到", e);
}
byte[] bytes = null;
try {
bytes = new byte[is.available()];
is.read(bytes);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
response.getOutputStream().write(bytes);
response.setContentType("xls");
response.flushBuffer(); } catch (IOException e) {
e.printStackTrace();
}
File file = new File(filepath);
if (file != null && file.isFile() && file.exists()) {
boolean b = file.delete();
if (b) {
logger.debug("删除临时文件成功");
} else {
logger.warn("删除临时文件失败");
}
}
} catch (UnsupportedEncodingException e) {
logger.error("转换按日期统计.xls为UTF-8编码失败", e);
} }//支持类import java.io.FileOutputStream;
import java.io.OutputStream;import org.apache.log4j.Logger;import jxl.Workbook;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;public class ExcelJxl {
private static Logger logger = Logger.getLogger(ExcelJxl.class);
/**
* excel格式导出
* @param alldata 组织好的数据
* @param outFileStr 文件输出路径
* @param tableName 表头名称
* @param index 表头截止位置x轴坐标起始点为0
*
*/
@SuppressWarnings("deprecation")
public static void getDateExcel(String[][] alldata,String outFileStr,String tableName,int index)
{
try
{
//创建小字体:Arial,大小为8号,非粗体,非斜体
WritableFont wf = new WritableFont(WritableFont.ARIAL, 8,WritableFont.NO_BOLD, false);
//创建大字体:Arial,大小为18号,粗体,非斜体
WritableFont Bwf = new WritableFont(WritableFont.ARIAL, 14,WritableFont.NO_BOLD, false);
//Bwf.setColour(jxl.format.Colour.RED);
//创建单元格格式:设置水平对齐为向右对齐
jxl.write.WritableCellFormat RwcfF = new jxl.write.WritableCellFormat(wf);
RwcfF.setAlignment(jxl.write.Alignment.RIGHT);
//创建单元格格式:设置水平对齐为向左对齐
jxl.write.WritableCellFormat LwcfF = new jxl.write.WritableCellFormat(wf);
LwcfF.setAlignment(jxl.write.Alignment.LEFT);
//创建单元格格式:设置水平对齐为居中对齐
jxl.write.WritableCellFormat CwcfF = new jxl.write.WritableCellFormat(wf);
CwcfF.setAlignment(jxl.write.Alignment.CENTRE);
jxl.write.WritableCellFormat CBwcfF = new jxl.write.WritableCellFormat(Bwf);
CBwcfF.setAlignment(jxl.write.Alignment.CENTRE);
//设置垂直对齐为居中对齐
CBwcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
//设置顶部边框线为实线(默认是黑色--也可以设置其他颜色)
CBwcfF.setBorder(jxl.format.Border.TOP, BorderLineStyle.MEDIUM);
//设置右边框线为实线
CBwcfF.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.MEDIUM);
//设置顶部框线为实线
CBwcfF.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.MEDIUM);
jxl.write.WritableCellFormat CMwcfF = new jxl.write.WritableCellFormat(wf);
CMwcfF.setAlignment(jxl.write.Alignment.LEFT);
//设置垂直对齐为向上对齐
CMwcfF.setVerticalAlignment(VerticalAlignment.TOP);
CMwcfF.setWrap(true);
// File tFile = new File(templatePath);//创建模板文件对象
//创建文件输出流对象
//FileOutputStream os = new FileOutputStream(outFile);
OutputStream os = new FileOutputStream(outFileStr);
//模板工作簿对象
//输出工作簿对象
WritableWorkbook wbook = Workbook.createWorkbook(os);
WritableSheet wsheet =wbook.createSheet("log", 0);
wsheet.setColumnView(0, 10); // 设置列的宽度
wsheet.setColumnView(1, 10); // 设置列的宽度
wsheet.setColumnView(2, 10); // 设置列的宽度
wsheet.setColumnView(3, 10); // 设置列的宽度
wsheet.setColumnView(4, 10); // 设置列的宽度
wsheet.setColumnView(5, 10); // 设置列的宽度
wsheet.mergeCells(0, 0, index, 0); // 合并单元格
wsheet.addCell(new Label(0, 0,tableName, CBwcfF));
for (int i = 0; i < alldata.length; i++)
{
for (int j = 0; j < alldata[i].length; j++)
{
wsheet.addCell(new Label(j, i+1, alldata[i][j], LwcfF));
if(alldata[i][j]!=null)
{
if(alldata[i][j].length()>wsheet.getColumnWidth(j))
{
wsheet.setColumnView(j, alldata[i][j].length()); // 设置列的宽度
}
}
}
}
// //在坐标为(0,0)的单元格写入"测试"字符串使用8号红色小字体,向右对齐
//
// //在坐标为(1,1)的单元格写入"test"字符串使用8号红色小字体,向左对齐
// wsheet.addCell(new Label(1, 1, "test", LwcfF));
// //在坐标为(2,2)的单元格写入"测试test"字符串使用8号红色小字体,居中对齐
// wsheet.addCell(new Label(2,2, "测试test", CMwcfF));
//写入
wbook.write();
wbook.close();
//关闭文件输出流
os.close();
}
catch(Exception e)
{
logger.error("创建excel jxl 异常",e);
}
}
这样就不需要jxl