response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment; filename=MyExcel.xls"); 
我在jsp中加上前的两行代码,数据是导出来了,可是没有格式,背景是白色的一片,请问如
把Excel中的表格线显示出来,谢谢!

解决方案 »

  1.   

    我的做法是先定制一个excel文件,另存为html,然后将html文件改为jsp,在其中嵌入jsp代码。
      

  2.   

    我试过了楼上的说的方法,这样连数据也导不出来了!!把jsp页面的内容给导出来了!!
      

  3.   

    楼主试一下下面的html文件 可直接运行
    但是只适合于IE内核的浏览器<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>将Web页面中的数据导出到Excel</title>
    <link href="CSS/style.css" rel="stylesheet">
    </head><body>
    <form name="form1" method="post" action="">  
    <table width="540" height="343" border="0" align="center" cellspacing="0" background="main.gif" >
        <tr align="center">
          <td width="42" height="56" align="right" valign="bottom"><img src="add.gif" width="20" height="18"></td>
          <td width="243" height="56" align="left" valign="bottom" id="doctitle">&nbsp;2007年1月份工资报表</td>
          <td width="249" align="left" valign="bottom">&nbsp;</td>
        </tr>
        <tr align="center">
          <td height="24"  colspan="3">&nbsp;</td>
        </tr>
        <tr align="center">
          <td colspan="3" valign="top">
    <table width="90%" border="0" cellspacing="1" bgcolor="#000000" id="wage">
          <tr>
            <td width="79" height="27" align="center" bgcolor="#efefef">员工名称</td>
            <td width="69" align="center" bgcolor="#efefef">奖金</td>
            <td width="104" align="center" bgcolor="#efefef">基本工资</td>
            <td width="63" align="center" bgcolor="#efefef">津贴</td>
            <td width="57" align="center" bgcolor="#efefef">扣除</td>
            <td width="93" align="center" bgcolor="#efefef">实发工资</td>
          </tr>
          <tr>
            <td width="79" height="27" align="center" bgcolor="#FFFFFF">wgh</td>
            <td width="69" align="center" bgcolor="#FFFFFF">200</td>
            <td width="104" align="center" bgcolor="#FFFFFF">1800</td>
            <td width="63" align="center" bgcolor="#FFFFFF">300</td>
            <td width="57" align="center" bgcolor="#FFFFFF">0</td>
            <td width="93" align="center" bgcolor="#FFFFFF">2300</td>
          </tr>
          <tr>
            <td width="79" height="27" align="center" bgcolor="#FFFFFF">sk</td>
            <td width="69" align="center" bgcolor="#FFFFFF">100</td>
            <td width="104" align="center" bgcolor="#FFFFFF">2100</td>
            <td width="63" align="center" bgcolor="#FFFFFF">210</td>
            <td width="57" align="center" bgcolor="#FFFFFF">5</td>
            <td width="93" align="center" bgcolor="#FFFFFF">2405</td>
          </tr>  
          <tr>
            <td width="79" height="27" align="center" bgcolor="#FFFFFF">春</td>
            <td width="69" align="center" bgcolor="#FFFFFF">70</td>
            <td width="104" align="center" bgcolor="#FFFFFF">1700</td>
            <td width="63" align="center" bgcolor="#FFFFFF">100</td>
            <td width="57" align="center" bgcolor="#FFFFFF">10</td>
            <td width="93" align="center" bgcolor="#FFFFFF">1860</td>
          </tr>  
          <tr>
            <td width="79" height="27" align="center" bgcolor="#FFFFFF">夏</td>
            <td width="69" align="center" bgcolor="#FFFFFF">100</td>
            <td width="104" align="center" bgcolor="#FFFFFF">1900</td>
            <td width="63" align="center" bgcolor="#FFFFFF">100</td>
            <td width="57" align="center" bgcolor="#FFFFFF">20</td>
            <td width="93" align="center" bgcolor="#FFFFFF">2080</td>
          </tr>
          <tr>
            <td width="79" height="27" align="center" bgcolor="#FFFFFF">秋</td>
            <td width="69" align="center" bgcolor="#FFFFFF">150</td>
            <td width="104" align="center" bgcolor="#FFFFFF">1600</td>
            <td width="63" align="center" bgcolor="#FFFFFF">100</td>
            <td width="57" align="center" bgcolor="#FFFFFF">0</td>
            <td width="93" align="center" bgcolor="#FFFFFF">1850</td>
          </tr>  
          <tr>
            <td width="79" height="27" align="center" bgcolor="#FFFFFF">冬</td>
            <td width="69" align="center" bgcolor="#FFFFFF">200</td>
            <td width="104" align="center" bgcolor="#FFFFFF">1500</td>
            <td width="63" align="center" bgcolor="#FFFFFF">100</td>
            <td width="57" align="center" bgcolor="#FFFFFF">0</td>
            <td width="93" align="center" bgcolor="#FFFFFF">1800</td>
          </tr>       
    </table>
    <p align="center"><a href="#" onClick="outExcel();">导出到Excel</a></p>  
      </td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    <script language="javascript">
    function outExcel(){
    var table=document.all.wage;
    row=table.rows.length;
    column=table.rows(1).cells.length;
    var excelapp=new ActiveXObject("Excel.Application");
    excelapp.visible=true;
    objBook=excelapp.Workbooks.Add(); //添加新的工作簿
    var objSheet = objBook.ActiveSheet; title=objSheet.Range("D1").MergeArea;  //合并单元格
    title.Cells(1,0).Value =doctitle.innerHTML.replace("&nbsp;","");  //输出标题
    title.Cells(1,1).Font.Size =16;
    for(i=1;i<row+1;i++){
    for(j=0;j<column;j++){
      objSheet.Cells(i+1,j+1).value=table.rows(i-1).cells(j).innerHTML.replace("&nbsp;","");
    }
    }
    excelapp.UserControl = true;
    }
    </script>
      

  4.   

    Automation不能创建,我要导出的这个excel没有固定的格式!
      

  5.   

    只要你本地能创建execl就可以做得到。
      

  6.   

    我建议你用POI 进行导出,可以控制样式,边框颜色阿 ,字体大小阿什么都可以通过代码来设置。http://376120937.qzone.qq.com/infocenter在我的日志里写的很详细你可以看看。或者代码直接粘过去跑一下看一下。
      

  7.   


    import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class FilePOIHSSFExecl { public void write(Reserve reserve_obj,List<ReserveDetail> reserveDtlList) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); 
    //建立新的SHEET
    HSSFSheet sheet = wb.createSheet("new sheet");
    //订单编号:(内容) 订单内容:(**)
    HSSFRow row = sheet.createRow((short)0);
    row.createCell((short)0).setCellValue("订单编号"); row.createCell((short)1).setCellValue(reserve_obj.订单编号); row.createCell((short)2).setCellValue("订单内容"); row.createCell((short)3).setCellValue(reserve_obj.订单内容); //订单时间:(**)  HSSFRow row2 = sheet.createRow((short)1); row2.createCell((short)0).setCellValue("订单时间"); row2.createCell((short)1).setCellValue(reserve_obj.订单时间); /*  订单详情编号 订单的时间 订单的什么什么的 内容 内容 内容 内容 内容 内容 */  //设置标题  HSSFRow row_dtl_title = sheet.createRow((short)2); row_dtl_title.createCell((short)0).setCellValue("订单详情编号"); row_dtl_title.createCell((short)1).setCellValue("订单的时间"); row_dtl_title.createCell((short)2).setCellValue("订单的什么什么的"); int line = 3; for (Iterator iter = list.iterator(); iter.hasNext(); ) { line++; ReserveDetail 订单详情 = iter.next(); if(订单详情 == null)continue; HSSFRow row_dtlvalue = sheet.createRow((short)line); row_dtlvalue.createCell((short)0).setCellValue(订单详情.订单详情编号); row_dtlvalue.createCell((short)1).setCellValue(订单详情.订单的时间); row_dtlvalue.createCell((short)2).setCellValue(订单详情.订单的什么什么的); } //inResponse.setHeader("Content-Disposition","inline; filename="+filename.toString()+".xls"); // Write the output to a file  wb.write(outputStream); outputStream.close(); } }
    这是用POI实现的一个例子,希望对LZ有所帮助。
      

  8.   


    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class FilePOIHSSFExecl {
        
    public void write(Reserve reserve_obj,List<ReserveDetail> reserveDtlList) throws IOException {
           HSSFWorkbook wb = new HSSFWorkbook();    
            
           //建立新的SHEET
           HSSFSheet sheet = wb.createSheet("new sheet");       //订单编号:(内容)  订单内容:(**) 
           HSSFRow row = sheet.createRow((short)0);
           row.createCell((short)0).setCellValue("订单编号");
           row.createCell((short)1).setCellValue(reserve_obj.订单编号);
     
           wb.write(outputStream);        
        
           outputStream.close();
          }}   刚才的格式有点乱,整理了下
      

  9.   

    我知道用jxl.jar,但我要导出的Excle没有固定的格式,不好控制,我就想知道你们用jsp导出时用什么方式去控制格式的?