0
votes

I am using APACHE POI 3.0 to add sheets to existing excel sheet. It works fine. But as APACHE POI has limitations about making charts, I used a template excel file to create charts, which also worked fine, but this always result in new excel file. If I have an existing excel sheet and I want to add a sheet, having charts, I am not able to do it. As, when I create charts, I use template file and it always makes a new excel file. so I was wondering if there is any solution of it of adding sheets to excel, where the sheets have charts

    public class TagBrowserSelection
{
    private static String[] excelBarPlot_Template       = { "","barPlot_1Panel_template.xlsx"};
    private static String[] excelPieChart_Template      = { "","pieChart_1Panel_template.xlsx"};
    private static String[] excelPieAndBarPlot_Template = { "","pieAndBarChart_1Panel_template.xlsx"};

    private static String   REGEX                       = "";

    static public boolean makeTagBrowserSelection(String strOutputFileName, ArrayList<TagBrowserChildPanel> childList, String sheetName, boolean addSheet, ArrayList<Boolean> chartAttributes)
    {
        // chart attributes
        boolean addBarChart = chartAttributes.get(0);
        boolean addPieChart = chartAttributes.get(1);
        boolean addNoTag = chartAttributes.get(2);
        boolean addZeros = chartAttributes.get(3);

        REGEX = "^" + sheetName;
        Pattern p = Pattern.compile(REGEX);

        String[] templateArray = null;
        if (addBarChart && addPieChart)
            templateArray = excelPieAndBarPlot_Template;
        else if (addBarChart)
            templateArray = excelBarPlot_Template;
        else if (addPieChart)
            templateArray = excelPieChart_Template;

        try
        {
            int number = childList.size();
            XSSFWorkbook workbook = null;
            XSSFWorkbook wb = null;
            XSSFSheet sheet = null;
            int col_num = 0;
            int row_num = 0;
            XSSFRow row = null;
            XSSFCell cell = null;
            // if adding sheet to existing excel file
            if (addSheet)
            {
                FileInputStream fis = new FileInputStream(new File(strOutputFileName));
                workbook = new XSSFWorkbook(fis);
                fis.close();

                // number of existing sheets in excel file
                int numberOfSheets = workbook.getNumberOfSheets();

                // check is sheetName exists already
                if (isSheetExist(sheetName, workbook))
                {
                    int counter = 1;
                    for (int ii = 0; ii < numberOfSheets; ii++)
                    {
                        Matcher m = p.matcher(workbook.getSheetName(ii));
                        if (m.find())
                            counter++;
                    }
                    sheetName = sheetName + " (" + counter + ")";
                }
            }
            else
            {
                workbook = new XSSFWorkbook();
            }

======================================================================

            // if template file needs to be used(if bar chart/pie chart option is selected)
            if (templateArray != null)
            {
                InputStream is = TagBrowserSelection.class.getClassLoader().getResourceAsStream(templateArray[number]);
                wb = new XSSFWorkbook(OPCPackage.open(is));
                sheet = wb.getSheetAt(0);
                // wb.close();
            }
            else
            {
                sheet = workbook.createSheet(sheetName);
            }

            // Freeze top two row
            // sheet.createFreezePane(0, 1, 0, 1);

            // Filling up the workbook and performing the row/column formatting
            for (TagBrowserChildPanel child : childList)
            {
                // Check if row is already created before(previous tag category)
                row = sheet.getRow(0);
                if (row == null)
                    row = sheet.createRow(0);

                // Adding tag category name as header
                String tagCategory = child.getSelectedCategory().getName();

                cell = row.createCell(col_num);
                cell.setCellValue(tagCategory);

                row = sheet.getRow(1);
                if (row == null)
                    row = sheet.createRow(1);

                // Adding column headers
                cell = row.createCell(col_num);
                cell.setCellValue("tag");
                cell = row.createCell(col_num + 1);
                cell.setCellValue("counts");

                row_num = 2;

                // Adding tag category document summary(name and counts)
                ArrayList<TagSummaryItem> tagSummary = child.getTagChartCounts();
                for (int i = 0; i < tagSummary.size(); i++)
                {
                    // Check if row is already created before(previous tag category)
                    row = sheet.getRow(row_num);
                    if (row == null)
                        row = sheet.createRow(row_num);

                    cell = row.createCell(col_num);
                    if (!addNoTag)
                    {
                        if (tagSummary.get(i).m_strTag == "[No Tag]")
                            continue;
                    }
                    if (!addZeros)
                    {
                        if (tagSummary.get(i).m_nCount == 0)
                            continue;
                    }
                    cell.setCellValue(tagSummary.get(i).m_strTag);
                    cell = row.createCell(col_num + 1);
                    cell.setCellValue(tagSummary.get(i).m_nCount);
                    row_num++;
                }
                // auto-size of tag column
                sheet.autoSizeColumn(col_num);

                col_num = col_num + 3;
            }

            FileOutputStream out = new FileOutputStream(strOutputFileName);

            if (templateArray != null)
            {
                wb.setSheetName(0, sheetName);
                wb.write(out);
                wb.close();
            }
            else
            {
                workbook.write(out);
                workbook.close();
            }
            out.close();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }

Above is my code, its one code. I split into two sections. Section is the one which uses template to make chart excel sheet.

1

1 Answers

0
votes

there's the method cloneSheet() in the HSSFWorkbook class. Try it.