如题,急

解决方案 »

  1.   

    写java类啊,,只放到过xml中。。我不会不好意思。
      

  2.   

    response.setContentType("application/vnd.ms-excel;charset=GB2312");这是以Excel显示 然后在不同位置写你要输出的代码
      

  3.   

    用poi可以的
    apache的一个开院项目
      

  4.   

    楼主没有说清楚是在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);
                wcf_center.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
                wcf_center.setAlignment(jxl.format.Alignment.CENTRE);
                wcf_center.setWrap(false);
                WritableCellFormat wcf_merge = new WritableCellFormat(BoldFont);
                wcf_merge.setBorder(jxl.format.Border.NONE, jxl.format.BorderLineStyle.THICK);
                wcf_merge.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
                wcf_merge.setAlignment(jxl.format.Alignment.CENTRE);
                wcf_merge.setWrap(true);
                HttpSession session = request.getSession(true);
                Vector print = (Vector)session.getAttribute("aa");
                String temp[][] = (String[][])print.get(0);
                String vBT = (String)print.get(1);
                String vXM = (String)print.get(2);
                int chang = Integer.parseInt(print.get(3).toString()) - 1;
                String strSPR = (String)print.get(4);
                Date systempDate = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String sysDate = format.format(systempDate);
                sheet.mergeCells(0, 0, chang, 2);
                sheet.addCell(new Label(0, 0, vBT, wcf_merge));
                sheet.mergeCells(0, 3, chang, 3);
                sheet.addCell(new Label(0, 3, "打印操作员:" + vXM, wcf_left));
                sheet.mergeCells(0, 4, chang, 4);
                sheet.addCell(new Label(0, 4, "打印时 间:" + sysDate, wcf_left));
                sheet.mergeCells(0, 5, chang, 5);
                sheet.addCell(new Label(0, 5, "审批 状态:" + strSPR, wcf_left));
                for(int m = 0; m < temp.length; m++)
                {
                    for(int n = 0; n < temp[m].length; n++)
                        sheet.addCell(new Label(n, m + 7, temp[m][n].replaceAll("\"", "").trim(), wcf_center));            }            workbook.write();
                workbook.close();
                bExportSucc = true;
            }
            catch(Exception e)
            {
                e.printStackTrace();
                sErrorStr = e.toString();
                System.out.println("在输出到EXCEL的过程中出现错误,错误原因:" + e.toString());
            }
            out.println("<html>");
            out.println("<head>");
            out.println("<title>陕西ItFutureEXCEL输出</title>");
            out.println("</head><body><br>");
            out.println("<div align=center>");
            if(bExportSucc)
                out.println("<meta http-equiv=refresh content=\"1;url=" + sUrlPath + "\">");
            else
                out.println("<font color=red>在输出到EXCEL文件的过程中发生错误,错误原因:" + sErrorStr + "</font>");
            out.println("</div></body></html>");
        }    public void destroy()
        {
        }
    }
      

  5.   

    比较取巧的办法是有的,因为html中的table,excel是可以自动识别并且显示成自己的表格的。你可以这样写jsp:
    <%
        java.util.Calendar now = java.util.Calendar.getInstance();
        String printFileName = "report.xls";
        String printHeader = "attachment; filename=" + printFileName;
        response.setContentType("application/force-download");
        response.setHeader("Content-disposition",printHeader);
    %>
    <html>
    <head>
    <title><%=printFileName%>
    <style type="text/css">
    body {
    font-size: 11px;
    font-family: Arial, Helvetica, sans-serif;
    }
    </style>
    </head>
    <body>
    <!--将下面内容替换为你的真正表格内容-->
    <table>
     <tr>
       <td>Column1</td><td>Column2</td>
     </tr>
     <tr>
       <td>data1</td><td>data2</td>
     </tr>
     <tr>
       <td>data3</td><td>data4</td>
     </tr>
    </table>
    <!---------------------------------->
    </body>
    </html>
      

  6.   

    用ireport还是不错的选择,还可以支持多种报表的生成
      

  7.   

    JExcel的话,这里有例子:
    http://www.java-tips.org/other-api-tips/jexcel/how-to-create-an-excel-file.htmlJExcel API provides various classes to create, read, write data to Excel documents at runtime. The required platform is JVM which means the code developed with JExcel can be run on Windows and Linux without any modification.The example below creates a new document and writes data into different sheets of the new Excel document.import java.io.*;
    import jxl.*;
    import java.util.*;
    import jxl.Workbook;
    import jxl.write.DateFormat;
    import jxl.write.Number;import jxl.write.*;
    import java.text.SimpleDateFormat;class  create
    {
      public static void main(String[] args) 
      {
        try
        {
          String filename = "input.xls";
          WorkbookSettings ws = new WorkbookSettings();
          ws.setLocale(new Locale("en", "EN"));
          WritableWorkbook workbook = 
            Workbook.createWorkbook(new File(filename), ws);
          WritableSheet s = workbook.createSheet("Sheet1", 0);
          WritableSheet s1 = workbook.createSheet("Sheet1", 0);
          writeDataSheet(s);
          writeImageSheet(s1);
          workbook.write();
          workbook.close();      
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
        catch (WriteException e)
        {
          e.printStackTrace();
        }
      }  private static void writeDataSheet(WritableSheet s) 
        throws WriteException
      {    /* Format the Font */
        WritableFont wf = new WritableFont(WritableFont.ARIAL, 
          10, WritableFont.BOLD);
        WritableCellFormat cf = new WritableCellFormat(wf);
        cf.setWrap(true);    /* Creates Label and writes date to one cell of sheet*/
        Label l = new Label(0,0,"Date",cf);
        s.addCell(l);
        WritableCellFormat cf1 = 
          new WritableCellFormat(DateFormats.FORMAT9);    DateTime dt = 
          new DateTime(0,1,new Date(), cf1, DateTime.GMT);    s.adCell(dt);
        
        /* Creates Label and writes float number to one cell of sheet*/
        l = new Label(2,0,"Float", cf);
        s.addCell(l);
        WritableCellFormat cf2 = new WritableCellFormat(NumberFormats.FLOAT);
        Number n = new Number(2,1,3.1415926535,cf2);
        s.addCell(n);    n = new Number(2,2,-3.1415926535, cf2);
        s.addCell(n);    /* Creates Label and writes float number upto 3 
           decimal to one cell of sheet */
        l = new Label(3,0,"3dps",cf);
        s.addCell(l);
        NumberFormat dp3 = new NumberFormat("#.###");
        WritableCellFormat dp3cell = new WritableCellFormat(dp3);
        n = new Number(3,1,3.1415926535,dp3cell);
        s.addCell(n);    /* Creates Label and adds 2 cells of sheet*/
        l = new Label(4, 0, "Add 2 cells",cf);
        s.addCell(l);
        n = new Number(4,1,10);
        s.addCell(n);
        n = new Number(4,2,16);
        s.addCell(n);
        Formula f = new Formula(4,3, "E1+E2");
        s.addCell(f);    /* Creates Label and multipies value of one cell of sheet by 2*/
        l = new Label(5,0, "Multipy by 2",cf);
        s.addCell(l);
        n = new Number(5,1,10);
        s.addCell(n);
        f = new Formula(5,2, "F1 * 3");
        s.addCell(f);    /* Creates Label and divide value of one cell of sheet by 2.5 */
        l = new Label(6,0, "Divide",cf);
        s.addCell(l);
        n = new Number(6,1, 12);
        s.addCell(n);
        f = new Formula(6,2, "F1/2.5");
        s.addCell(f);
      }  private static void writeImageSheet(WritableSheet s) 
        throws WriteException
      {
        /* Creates Label and writes image to one cell of sheet*/    
        Label l = new Label(0, 0, "Image");
        s.addCell(l);
        WritableImage wi = new WritableImage(0, 3, 5, 7, new File("image.png"));
        s.addImage(wi);    /* Creates Label and writes hyperlink to one cell of sheet*/
        l = new Label(0,15, "HYPERLINK");
        s.addCell(l);
        Formula f = new Formula(1, 15, 
          "HYPERLINK(\"http://www.andykhan.com/jexcelapi\", "+
          "\"JExcelApi Home Page\")");
        s.addCell(f);
        
        }
    }