java导出excel图表数据

导出excel图表

因为客户的要求,需要在导出报表时Excel中要有图表,图表要跟据数据的条数来自动增加。因为项目组内之前没有人做过,一开始大家方向统一都转向用Java操作excel的宏来实现,用excel宏实现必须懂VB,这样就提高了开发成本和维护成本。最后经研究绝定将图表做成一个模板,java程序向模板里添加数据,通过Excel的函数来设置图表的数据。

excel图表模板的制作

例如我们做一下季度报表

1.先在excel中写入一些数据,选中写入的所有数据,插入——>柱状图


2.在生成的柱状图上右击——>选择数据——>点击切换行/列按钮——>确定

 

 

3.点击公式——>名称管理——>新建

 


注:公式称必须是英文名称

引用位置中  Sheet1 是当前sheet页的名称  $A$2表示A列第2行开始

依次将所有列都新建


 

4.依次选中山东省分行上的柱子,在公式框中依次输入

=SERIES("一季度",Sheet1!dept,Sheet1!que_1,1)

=SERIES("一季度",Sheet1!dept,Sheet1!que_2,2)

=SERIES("一季度",Sheet1!dept,Sheet1!que_3,3)

=SERIES("一季度",Sheet1!dept,Sheet1!que_3,3)

 

这样模板就做好了,我们只需要在表格上新增一条数据,图表就会相应的更新

 

 

 

POI根据模板导出EXCEL

public class ModuleExcel implements IModuleExcel
{
       /*
       *excelTemplatePath 模板名称
       *outputStream 输出流
       *cellList  数据LIST List<Map<String,Object>>
       *titleList 数据Map里的Key
       */

       public void writeExcel(String excelTemplatePath,OutputStream outputStream,
              List<Map<String,Object>> cellList,String[] titleList)
       {

              POIFSFileSystem modelook = null;

              HSSFWorkbook book = null;

              HSSFSheet sheet = null;
              try
              {
                     if(excelTemplatePath!=null)
                     {
                            //加载模板 模板名称 /export/YearChartForScore.xls
                            //final InputStream in =(IModuleExcel.class).getClassLoader().getResourceAsStream(excelTemplatePath);
                            final InputStream in = new FileInputStream(excelTemplatePath);
                            modelbook = new POIFSFileSystem(in);
                     }
                     book = new HSSFWorkbook(modelbook);
                     sheet = book.getSheetAt(0);
                     //向Excel中写入数据
                     for(int j=0; j<cellList.size(); j++)
                     {
                            HSSFRow row = sheet.createRow(j+1);
                            Map<String,Object> dataMap = cellList.get(j);
                            for(int i=0; i< titleList.length; i++)
                            {
                                   String titleName = titleList[i];
                                   HSSFCell cell = row.createCell((short)i);
                                   if(i!=0)
                                   {
                                          cell.setCellValue(Integer.parseInt(dataMap.get(titleName)==null ? 0 : dataMap.get(titleName.toString()));
                                   }else
                                   {
                                          cell.setCellValue(dataMap.get(titleName)==null ? "" :dataMap.get(titleName).toString());
                                   }
                            }
                     }

                     book.write(outputStream);
                     outputStream.close();
              }catch(Exception e)
              {
                     outputStream.close();
                     e.printStackTrace();
              }
       }
 

       public static void main(String[] args)

       {
              File file = new File("D:/report.xls");
              OutputStream outputStream = null;
              if(!file.exists())
              {
                     try
                     {
                            file.createNewFile();
                            outputStream = new FileOutputStream(file);
                     }catch(final Exception e){
                            e.printStackTrace();
                     }
              }
 
              String[] titleList =  new String[]{"dept","que1","que2","que3","que4"};
              List<Map<String,Objct>> cellList = new ArrayList<Map<String,Object>>();
              Map<String,Object> data1 = new HashMap<String,Object>();
              data1.put("dept","总行");
              data1.put("que1",20);
              data1.put("que2",30);
              data1.put("que3",10);
              data1.put("que4",15);

              Map<String,Object> data2 = new HashMap<String,Object>();
              data2.put("dept","山东省分行");
              data2.put("que1",18);
              data2.put("que2",28);
              data2.put("que3",8);
              data2.put("que4",13);

 
              Map<String,Object> data3 = new HashMap<String,Object>();
              data3.put("dept","山西省分行");
              data3.put("que1",10);
              data3.put("que2",30);
              data3.put("que3",22);
              data3.put("que4",16);
              cellList.add(data1);
              cellList.add(data2);
              cellList.add(data3);
              ModuleExcel excel = new ModuleExcel();
              excel.writeExcel("D:/excel.xls",outputStream,cellList,titleList);

       }

}


导出结果


 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值