求EXCEL导出的代码 我想将网页中的table表格导出到EXCEL中谁有源代码啊 帮个忙啊 十分感谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 想必你的table里面的值也是db里面的值。我觉得做个Excel魔板,然后直接用poi或第三方库写Excel就可以了,然后Download下来 response.setContentType("applicationpeng/ms-download");response.setHeader("Content-Disposition", "attachment;filename=Report.xls");String text="<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"></head><body><TABLE border=1 align=center cellPadding=2 cellSpacing=1 width='100%'>"+request.getParameter("context")+"</table></body></html>";response.getWriter().println(text);response.getWriter().close();前一个页面放个span叫context。 偶用过POI 感觉还不错 lz试试 google baidu下好多这个例子~ POI还可以代码片断如下: 写Excel文件: private HSSFSheet ws = null; HSSFRow xlsRow = ws.createRow(row); HSSFCell cell = xlsRow.createCell((short) col); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(nodeText);web方式下载文件: public void generateXlSFile(HttpServletResponse response) { String rootId = null; rootId = this.getRootId(); try { BufferedOutputStream bos = new BufferedOutputStream(response .getOutputStream()); //FileOutputStream fos = new FileOutputStream(fileName); HSSFWorkbook wb = new HSSFWorkbook(); ws = wb.createSheet(); wb.setSheetName(0, this.getFileName(), (short) 1); //this.traveNode(rootId, 1, 1); wb.write(bos); bos.close(); } catch (IOException e) { // } }更详细的可以查阅POI的相关文档 貌似poi,jexcel都很不错,可以试试。 要支持rowSpan和colSpan还是挺麻烦的,还有数字右对齐等,pio可实现,我同事写了个,由于不是我写的不能贴出来呵呵,但是可以跟你说下,使用outerHTML替换些不支持的属性后分析下colSpan和rowSpan,有个对象可以干这事,最后把生成的流流出来就行了. 这里有个servlet的代码public class ApplesAndOranges extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/vnd.ms-excel"); PrintWriter out = response.getWriter(); out.println("\tQ1\tQ2\tQ3\tQ4\tTotal"); out.println("Apples\t78\t87\t92\t29\t=SUM(B2:E2)"); out.println("Oranges\t77\t86\t93\t30\t=SUM(B3:E3)"); }}其实关键就是response.setContentType("application/vnd.ms-excel"); 你自己体会一下 关于导航栏的问题 最近WEBWORK2老报这个错,郁闷,本地测试都没这个问题 jsp出错,各位高手进来指点一下,小弟在这里24在线等!!!!急呀 解决了码上给分? 超简单表单自动校验 总是出现报错页面(tomcat4.1.18+eclipse+oracle)(在等。。。) TOMCAT5和IIS5的整合,JSP能用,SERVLET不行 关于过滤器的一些想法!可能不太成熟,希望高手指点??? 什么错误?急 ****请教各位高人怎么实现点在jsp页面上点击链接后调用一个外部的exe文件?保证给分! JAVA实现CSV文件下载问题 数据库写入和读取的问题
response.setHeader("Content-Disposition", "attachment;filename=Report.xls");
String text="<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"></head><body><TABLE border=1 align=center cellPadding=2 cellSpacing=1 width='100%'>"+request.getParameter("context")+"</table></body></html>";
response.getWriter().println(text);
response.getWriter().close();
前一个页面放个span叫context。
代码片断如下:
写Excel文件:
private HSSFSheet ws = null;
HSSFRow xlsRow = ws.createRow(row);
HSSFCell cell = xlsRow.createCell((short) col);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(nodeText);
web方式下载文件:
public void generateXlSFile(HttpServletResponse response) { String rootId = null;
rootId = this.getRootId();
try { BufferedOutputStream bos = new BufferedOutputStream(response
.getOutputStream()); //FileOutputStream fos = new FileOutputStream(fileName);
HSSFWorkbook wb = new HSSFWorkbook();
ws = wb.createSheet();
wb.setSheetName(0, this.getFileName(), (short) 1);
//this.traveNode(rootId, 1, 1);
wb.write(bos);
bos.close(); } catch (IOException e) {
//
}
}
更详细的可以查阅POI的相关文档
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("application/vnd.ms-excel");
PrintWriter out = response.getWriter();
out.println("\tQ1\tQ2\tQ3\tQ4\tTotal");
out.println("Apples\t78\t87\t92\t29\t=SUM(B2:E2)");
out.println("Oranges\t77\t86\t93\t30\t=SUM(B3:E3)");
}
}其实关键就是response.setContentType("application/vnd.ms-excel");
你自己体会一下