日期:2014-05-18  浏览次数:20706 次

我想把查询到的记录集生成Excel表格,请问在JSP里该怎么做????
如题,急

------解决方案--------------------
楼主没有说清楚是在jsp页面中用excel方式显示还是写入一个excel文件;

1、jsp显示方式;加入代码:response.setContentType( "application/vnd.ms-excel;charset=GB2312 ");

2、写入一个excel文件;比较麻烦,需要第三方工具,不过网上很多,可以去下载,如apache的poi,sourceforge的jxl,地址:http://sourceforge.net/project/showfiles.php?group_id=79926;等,但是需要你自己去了解相关的api用法:


我以前的部分参考代码:
--------------------------------------------
package com.Workflow.Netoa.Manage;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.*;
import jxl.write.*;

public class PrinterServlet extends HttpServlet
{

private static final String CONTENT_TYPE = "text/html; charset=GBK ";

public PrinterServlet()
{
}

public void init()
throws ServletException
{
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType( "text/html; charset=GBK ");
PrintWriter out = response.getWriter();
boolean bExportSucc = false;
String sErrorStr = " ";
String sHead = "/files/ ";
String sFileName = "Test.xls ";
String sCurrPath = request.getRealPath( "/files/ ");
File filepath = new File(sCurrPath);
if(filepath.exists())
out.println( "路径已经存在! <br> ");
else
filepath.mkdir();
sCurrPath = sCurrPath + "/ " + sFileName;
File file = new File(sCurrPath);
file.createNewFile();
String sUrlPath = sHead + sFileName;
try
{
WritableWorkbook workbook = Workbook.createWorkbook(new File(sCurrPath));
WritableSheet sheet = workbook.createSheet( "陕西ItFuture信息管理系统 ", 0);
SheetSettings sheetset = sheet.getSettings();
sheetset.setProtected(false);
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
wcf_left.setBorder(jxl.format.Border.NONE, jxl.format.BorderLineStyle.THIN);
wcf_left.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
wcf_left.setAlignment(jxl.format.Alignment.LEFT);
wcf_left.setWrap(false);
WritableCellFormat wcf_right = new WritableCellFormat(NormalFont);
wcf_right.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
wcf_right.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf_right.setAlignment(jxl.format.Alignment.RIGHT);
wcf_right.setWrap(false);
WritableCellFormat wcf_center = new WritableCellFormat(NormalFont);
wcf_center.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);