java 将数据导出成Excel文件

maven jar包的引用:
<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.5.7</version>
</dependency>
String title[]={"项目","实例","IP","应用","集群","部门","时间","CPU","内存","估价","监控","利用率","最高"};String data[]={"Project","Instance","IP","Application","Cluster","Department","Date","CPU","Memory","Price","Name1","Analysis","Max"};YJServerRelation model=(YJServerRelation)getSession().getAttribute("resourceModel");List<YJServerRelation> list=server_relation_svc.listFull(model, this);//操作执行try { HttpServletResponse httpServletResponse=this.getResponse(); ServletOutputStream os=httpServletResponse.getOutputStream(); httpServletResponse.reset(); httpServletResponse.setHeader("Content-disposition", "attachment; filename=utilization.csv"); httpServletResponse.setContentType("application/msexcel;charset=utf-8"); //创建excel文件 WritableWorkbook book= Workbook.createWorkbook(os); //生成名为“第一页”的工作表 WritableSheet sheet=book.createSheet("第一页",1); //写入标题 for(int index=0;index<title.length;index++) //title sheet.addCell(new Label(index,0,title[index])); //写入内容 for(int columns=0;columns<data.length;columns++)//列 { for(int rows=0;rows<list.size(); rows++)//行 { sheet.addCell(new Label(columns,rows+1,(list.get(rows).getStr(data[columns])))); } } //写入数据 book.write(); //关闭文件 book.close();}catch(Exception e) { //失败}
以下的java代码可直接在eclipse运行,但需要添加jxl的jar包:
package test;
import java.io.File;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
 
public class OutExcel{
  public static void main(String[] args) {
    //标题行
    String title[]={"角色","编号","功能名称","功能描述"};
    //内容
    String context[][]={{"UC11","设置课程","创建课程"},
              {"UC12","设置学生名单","给出与课程关联的学生名单"},
              {"UC21","查看学生名单",""},
              {"UC22","查看小组信息","显示助教所负责的小组列表信息"}
              };
    //操作执行
    try { 
      //t.xls为要新建的文件名
      WritableWorkbook book= Workbook.createWorkbook(new File("C:\\Users\\Administrator\\Desktop\\t.xls")); 
      //生成名为“第一页”的工作表,参数0表示这是第一页 
      WritableSheet sheet=book.createSheet("第一页",0); 
       
      //写入内容
      for(int i=0;i<4;i++)  //title
        sheet.addCell(new Label(i,0,title[i])); 
      for(int i=0;i<4;i++)  //context
      {
        for(int j=0;j<3;j++)
        {
          sheet.addCell(new Label(j+1,i+1,context[i][j])); 
        }
      }
      sheet.addCell(new Label(0,1,"教师"));
      sheet.addCell(new Label(0,3,"助教"));
       
      /*合并单元格.合并既可以是横向的,也可以是纵向的
       *WritableSheet.mergeCells(int m,int n,int p,int q);  表示由(m,n)到(p,q)的单元格组成的矩形区域合并
       * */
      sheet.mergeCells(0,1,0,2);
      sheet.mergeCells(0,3,0,4);
      sheet.insertColumn(0);
      sheet.addCell(new Label(0,4,"合计"));
       
      //写入数据
      book.write(); 
      //关闭文件
      book.close(); 
    }
    catch(Exception e) { } 
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值