在Excel工作表中插入动态图表

时间:2017-12-01 06:34:22

标签: java excel spring charts apache-poi

我正在制作一个分析应用程序,基本上从数据库中取出数据,点击一个按钮就可以下载包含该数据的Excel工作表。我已经使用了apace poi 3.10。我还要求在excel表中创建与该数据对应的图表。我需要一些指导。

这是我在excel中插入数据的代码。我需要在这里添加一些东西以获得与数据相对应的图表:

package com.DAOPackage;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.dataType.SubmittedProposal;
import com.dataType.SubmittedProposalPerProduct;
import com.dataType.SubmittedProposalYearly;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;

@Component
public class ExcelBuilder extends AbstractExcelView {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        // get data model which is passed by the Spring container
        List<SubmittedProposal> submittedProposalMonthly = (List<SubmittedProposal>) model
                .get("submittedProposalProductMonthly");
        List<SubmittedProposalYearly> submittedProposalYearly = (List<SubmittedProposalYearly>) model
                .get("submittedProposalYearly");
        List<SubmittedProposalPerProduct> submittedProposalPerProduct = (List<SubmittedProposalPerProduct>) model
                .get("submittedProposalPerProduct");
        BigDecimal submittedProposal = (BigDecimal) model.get("submittedProposal");
        BigDecimal activeUser = (BigDecimal) model.get("activeUser");

        // Sheet for Submitted Monthly Proposal
        // create a new Excel sheet
        HSSFSheet sheet = workbook.createSheet("Submitted Proposal Monthly");
        sheet.setDefaultColumnWidth(30);

        // create style for header cells
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("Arial");
        style.setFillForegroundColor(HSSFColor.BLUE.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.WHITE.index);
        style.setFont(font);

        // create header row
        HSSFRow header = sheet.createRow(0);

        header.createCell(0).setCellValue("Month");
        header.getCell(0).setCellStyle(style);

        header.createCell(1).setCellValue("Number");
        header.getCell(1).setCellStyle(style);

        // create data rows
        int rowCount = 1;

        for (SubmittedProposal submittedProposalMonth : submittedProposalMonthly) {
            HSSFRow aRow = sheet.createRow(rowCount++);
            aRow.createCell(0).setCellValue(submittedProposalMonth.getDate());
            aRow.createCell(1).setCellValue(submittedProposalMonth.getTotal());
        }

        // Sheet for Submitted Yearly Proposal
        // create a new Excel sheet
        HSSFSheet sheet2 = workbook.createSheet("Submitted Proposal Yearly");
        sheet2.setDefaultColumnWidth(30);

        // create header row
        HSSFRow header2 = sheet2.createRow(0);

        header2.createCell(0).setCellValue("Year");
        header2.getCell(0).setCellStyle(style);

        header2.createCell(1).setCellValue("Number");
        header2.getCell(1).setCellStyle(style);

        // create data rows
        int rowCount2 = 1;

        for (SubmittedProposalYearly submittedProposalYear : submittedProposalYearly) {
            HSSFRow aRow = sheet2.createRow(rowCount2++);
            aRow.createCell(0).setCellValue(submittedProposalYear.getYear());
            aRow.createCell(1).setCellValue(submittedProposalYear.getTotal());
        }

        // Sheet for Active User
        // create a new Excel sheet
        HSSFSheet sheet3 = workbook.createSheet("Active Users");
        sheet3.setDefaultColumnWidth(30);

        // create header row
        HSSFRow header3 = sheet3.createRow(0);

        header3.createCell(0).setCellValue("Total Active User");
        header3.getCell(0).setCellStyle(style);

        // create data rows

        String users = String.valueOf(activeUser);
        HSSFRow aRow1 = sheet3.createRow(1);
        aRow1.createCell(0).setCellValue(users);

        // Sheet for Submitted Proposal
        // create a new Excel sheet
        HSSFSheet sheet4 = workbook.createSheet("Submitted Proposal");
        sheet4.setDefaultColumnWidth(30);

        // create header row
        HSSFRow header4 = sheet4.createRow(0);

        header4.createCell(0).setCellValue("Total Submitted Proposal");
        header4.getCell(0).setCellStyle(style);

        // create data rows

        String submitProposal = String.valueOf(submittedProposal);
        HSSFRow aRow2 = sheet4.createRow(1);
        aRow2.createCell(0).setCellValue(submitProposal);


        // Sheet for Submitted Proposal Per Product
                // create a new Excel sheet
                HSSFSheet sheet5 = workbook.createSheet("Submitted Proposal Per Product");
                sheet5.setDefaultColumnWidth(30);

                // create header row
                HSSFRow header5 = sheet5.createRow(0);

                header5.createCell(0).setCellValue("Month");
                header5.getCell(0).setCellStyle(style);

                header5.createCell(1).setCellValue("Product");
                header5.getCell(0).setCellStyle(style);

                header5.createCell(2).setCellValue("Number");
                header5.getCell(1).setCellStyle(style);

                // create data rows
                int rowCount3 = 1;

                for (SubmittedProposalPerProduct submitProposalPerProduct : submittedProposalPerProduct) {
                    HSSFRow aRow = sheet5.createRow(rowCount3++);
                    aRow.createCell(0).setCellValue(submitProposalPerProduct.getMonth());
                    aRow.createCell(1).setCellValue(submitProposalPerProduct.getProduct());
                    aRow.createCell(2).setCellValue(submitProposalPerProduct.getTotal());
                }
    }
}

0 个答案:

没有答案