返回目录:excel表格制作
java中导出Excel有两个组件可以使用,一个是jxl,一个是POI,我这里用的是POI。导出是可以在服务器上生成文件,然后下载,也可以利用输出流直接在网页
中弹出对话框提示用户保存或下载。生成文件的方式会导致服务器中存在着垃圾文件,实现方式不太优雅,所以这里我采用的是后面直接通过输出流的方式。
1、修改e799bee5baa6e997aee7ad94e59b9ee7ad94362WEB服务器的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,复杂的应用请查看API
package 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
jsp要从mysql导出数据到excel分两步操作:
后台查询mysql符合条件的数据,放在session中。e79fa5e98193e59b9ee7ad94333
页面展示数据,并且控制导出,添加一个按钮,调用导出方法执行。
举例说明:
Java代码
sql = "select * from tablename";
rs = stmt.executeQuery(sql);
//新建Excel文件
String filePath=request.getRealPath("aaa.xls");
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
resultFile.close();
//用JXL向新建的文件中添加内容
OutputStream outf = new FileOutputStream(filePath);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(outf);
jxl.write.WritableSheet ws = wwb.createSheet("sheettest", 0);
int i=0;
int j=0;
for (int k = 0; k < rs.getMetaData().getColumnCount(); k++) {
ws.addCell(new Label(k,0,rs.getMetaData().getColumnName(k+1)));
}
while(rs.next()){
out.println(rs.getMetaData().getColumnCount());
for (int k = 0; k < rs.getMetaData().getColumnCount(); k++) {
ws.addCell(new Label(k,j+i+1,rs.getString(k+1)));
}
i++;
}
wwb.write();
wwb.close();
}catch(Exception e){e.printStackTrace();}
finally{
rs.close();
conn.close();
}
response.sendRedirect("aaa.xls");
var好像是js里的不是java的,你代码参考错了
2、 行高百和列宽
WritableSheet.setRowView(int i,int height);
作用是指定第度i+1行的高度回,比答如:
//将第一行的高度设为200
sheet.setRowView(0,200);
WritableSheet.setColumnView(int i,int width);