数据导入到excel并显示在页面

    技术2022-05-11  25

     

    完全用jxl包首先写java文件package com.test.excel;import java.io. * ;import jxl. * ;import jxl.write. * ;import jxl.format. * ;import java.util. * ;import java.awt.Color; public   class  TestExcel  {    public static void writeExcel(OutputStream os) throws Exception {        WritableWorkbook wwb = Workbook.createWorkbook(os);        WritableSheet ws = wwb.createSheet("TestSheet1"0);        Label labelC = new Label(00"哈哈");        ws.addCell(labelC);        WritableFont wfc = new WritableFont(WritableFont.ARIAL,20, WritableFont.BOLD, false,                UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);        WritableCellFormat wcfFC = new WritableCellFormat(wfc);        wcfFC.setBackground(jxl.format.Colour.RED);        labelC = new Label(41"gagaga呷呷机阿姐阿",wcfFC);        ws.addCell(labelC);                for(int i = 5;i<9;i++){   //循环操作写单元格            for(int j=0;j<4;j++){                labelC = new Label(j, i, i+""+j);                ws.addCell(labelC);            }        }                // 写入Exel工作表        wwb.write();        // 关闭Excel工作薄对象        wwb.close();    }    // 最好写一个这样的main方法来测试一下你的这个class是否写好了。    public static void main(String[] args)throws Exception{        File f=new File("kk.xls");        f.createNewFile();        writeExcel(new FileOutputStream(f));    }} 然后在页面显示:excel,jsp <% @page import = " com.test.excel.TestExcel "   %> <%     response.reset();    response.setContentType( " application/vnd.ms-excel " );    TestExcel.writeExcel(response.getOutputStream()); %>  

    最新回复(0)