如何用Java实现导出excel 如题·~ 在网上找了几个都用不成 希望各位大侠们 能把代码贴出来 最好有注释的~~ 不甚感激~~ 在线等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用POI就可以了,不过建议直接导出csv文件,用excel打开就可以了,这样效率会高。 1.下载poi的包包// createXls(存放路径如:(c:\test.xls),要导出的东西,excel的头一行)2.public void createXls(String filePath,List<对象> phList,String[] headers) throws Exception { // 建工作薄 HSSFWorkbook wb = new HSSFWorkbook(); // 建名为example的工作表 HSSFSheet sheet = wb.createSheet("测试"); System.out.println("abc"); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth(15); // 产生表格表头 HSSFRow headerrow = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { HSSFCell cell = headerrow.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } Iterator<对象> it = (Iterator<对象>) phList.iterator(); int index = 0; while (it.hasNext()) { index++; HSSFRow datarow = sheet.createRow(index); Phone a = it.next(); HSSFCell data1 = datarow.createCell(0); HSSFRichTextString richString1 = new HSSFRichTextString(a.get属性()); data1.setCellValue(richString1); } FileOutputStream fileout = new FileOutputStream(filePath); wb.write(fileout); fileout.close(); } http://www.java2000.net/p1681 用jxl吧 一个类包 网上关于它的说明应该很多 但是这个东西比较耗资源 需要一个jxl.jar;代码如下package com.webapp.officialdocument.filing.action;import java.io.File;import java.io.OutputStream;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableFont;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import com.webapp.app.util.DateUtils;import com.webapp.core.common.domain.PageInfo;import com.webapp.officialdocument.filing.domain.Filing;import com.webapp.officialdocument.filing.iservice.IFilingService;import com.webapp.struct.module.action.EmpowermentAction;public class FilingExcelAction extends EmpowermentAction{ private IFilingService service; public void setService(IFilingService service) { this.service = service; } public void execute(HttpServletRequest req, HttpServletResponse resp, Map views) throws Exception { String fileName = "filing"; String zbmc = fileName; OutputStream os = resp.getOutputStream(); resp.setContentType("application/ms-excel;charset=utf-8"); resp.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") + ".xls"); Map search = (Map) views.get("search"); long old = (long)(System.currentTimeMillis()/86400000); if(search.get("filingdate2").toString().equals(old+"")) { search.put("filingdate2", System.currentTimeMillis()); } PageInfo pageInfo = (PageInfo) views.get("pageInfo"); List list = service.list(search, pageInfo); { try { WritableWorkbook book = Workbook.createWorkbook(os); WritableSheet sheet = book.createSheet("Sheet_1", 0); Label label = new Label(0, 0, "档案编号"); Label label1 = new Label(1, 0, "公文编号"); Label label2 = new Label(2, 0, "公文标题"); Label label3 = new Label(3, 0, " 公文分类"); Label label4 = new Label(4, 0, "归档时间"); Label label5 = new Label(5, 0, "保存时间"); Label label6 = new Label(6, 0, "归档姓名"); Label label7 = new Label(7, 0, "公文作者"); Label label8 = new Label(8, 0, "档案分类"); sheet.addCell(label); sheet.addCell(label1); sheet.addCell(label2); sheet.addCell(label3); sheet.addCell(label4); sheet.addCell(label5); sheet.addCell(label6); sheet.addCell(label7); sheet.addCell(label8); catch (Exception e) { e.printStackTrace(); } } }} 10楼写得很详细啦,其实JXL跟POI的写法都差不多,看自己习惯或喜欢用哪个了 如果数据是从数据库取得,oracle建议用ociuldr导出,sqlserver用BCP导出,使用数据库工具导出,速度会非常快 关于mybatis中@SelectProvider的一点疑惑 跳转过去没有反应? 在mysql5.0中不能插入中文数据 谁有jena? 这是什么错误啊?no protocol : hibernate-configuration-3.0 dtd 请教各位大虾一个java基础问题结合spring 关于JBOSS开发EJB3。0部署问题 急问struts中treeview中显示问题,恳请struts中做过treeview的高手提醒!! 高分挑战高手:jsp访问ejb 高分求学习java的书籍和方法以及资料,up有分 struts2一个简单显示问题 struts2+jasperreport 得不到值
不过建议直接导出csv文件,用excel打开就可以了,这样效率会高。
2.public void createXls(String filePath,List<对象> phList,String[] headers) throws Exception {
// 建工作薄
HSSFWorkbook wb = new HSSFWorkbook(); // 建名为example的工作表
HSSFSheet sheet = wb.createSheet("测试");
System.out.println("abc");
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);
// 产生表格表头
HSSFRow headerrow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = headerrow.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
Iterator<对象> it = (Iterator<对象>) phList.iterator();
int index = 0;
while (it.hasNext()) {
index++;
HSSFRow datarow = sheet.createRow(index);
Phone a = it.next();
HSSFCell data1 = datarow.createCell(0);
HSSFRichTextString richString1 = new HSSFRichTextString(a.get属性());
data1.setCellValue(richString1);
}
FileOutputStream fileout = new FileOutputStream(filePath);
wb.write(fileout);
fileout.close();
}
import java.io.OutputStream;
import java.util.List;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;import com.webapp.app.util.DateUtils;
import com.webapp.core.common.domain.PageInfo;
import com.webapp.officialdocument.filing.domain.Filing;
import com.webapp.officialdocument.filing.iservice.IFilingService;
import com.webapp.struct.module.action.EmpowermentAction;public class FilingExcelAction extends EmpowermentAction
{
private IFilingService service;
public void setService(IFilingService service) {
this.service = service;
}
public void execute(HttpServletRequest req, HttpServletResponse resp,
Map views) throws Exception
{
String fileName = "filing";
String zbmc = fileName;
OutputStream os = resp.getOutputStream();
resp.setContentType("application/ms-excel;charset=utf-8");
resp.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") + ".xls");
Map search = (Map) views.get("search");
long old = (long)(System.currentTimeMillis()/86400000);
if(search.get("filingdate2").toString().equals(old+""))
{
search.put("filingdate2", System.currentTimeMillis());
}
PageInfo pageInfo = (PageInfo) views.get("pageInfo");
List list = service.list(search, pageInfo);
{
try {
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet = book.createSheet("Sheet_1", 0);
Label label = new Label(0, 0, "档案编号");
Label label1 = new Label(1, 0, "公文编号");
Label label2 = new Label(2, 0, "公文标题");
Label label3 = new Label(3, 0, " 公文分类");
Label label4 = new Label(4, 0, "归档时间");
Label label5 = new Label(5, 0, "保存时间");
Label label6 = new Label(6, 0, "归档姓名");
Label label7 = new Label(7, 0, "公文作者");
Label label8 = new Label(8, 0, "档案分类");
sheet.addCell(label);
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
sheet.addCell(label6);
sheet.addCell(label7);
sheet.addCell(label8);
catch (Exception e)
{
e.printStackTrace();
}
}
}}