作者:乔山办公网日期:
返回目录:excel表格制作
报格式错误是因为你没有填充EXCEL的内容度。
正确的做法是:
1, HSSFWorkbook ws = new HSSFWorkbook();//建立新HSSFWorkbook对象版
2, Sheet sheet = workbook.createSheet(0); //建立一个新的sheet
3,Row row = sheet.createRow(1); //建立一个新的row对象
4, Cell cell = row.createCell(0); //在row上创建方格即列,
cell.setCellValue(cellValue); //设置这个行中列的值
cell.setCellStyle(cellStyle); //设置样式权
String temFilePath = getServletContext().getRealPath("/") + "uploadfiles/" + tmpXlsName;大概代码就是这样,根据你的模板,生成文件后,。。。
File file = new File(temFilePath);
InputStream input =new FileInputStream(file);
Workbook wb = WorkbookFactory.create(input); // 根据模板生成新的excel文件7a686964616fe78988e69d83335
input.close();
Sheet sheet = wb.getSheetAt(0);
while (rs.next()) {
Row row =sheet.getRow(++rowIdx);
if(row==null){
row =sheet.createRow(rowIdx);
}
for (int cIdx=Integer.parseInt(colStartIdx)-1,colIdx=1; cIdx<Integer.parseInt(colEndIdx); cIdx++,colIdx++) {
Object value = rs.getObject(colIdx);
Cell cell = row.createCell(cIdx);
cell.setCellValue(value==null?"":value.toString());
cell.setCellStyle(cs);
}
}
java中导出Excel有两个组件可以使用,一个是jxl,一个是POI,我这里用的是POI。导出是可以在服务器上生成文件,然后下载,也可以利用输出流直接在网页
中弹出对话框提示用户保存或下载。生成文件的方式会导致服务器中存在着垃圾文件,实现方式不太优雅,所以这里我采用的是后面直接通过输出流的方式。
1、修改WEB服务器的CONF/web.xml,添加e5a48de588b6e79fa5e98193362
<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
是不是你的excel中没有设置公式自动计算