java - 使用java在Excel工作表中创建数据透视表

标签 java excel apache-poi pivot-table

如何使用java在数据透视表中设置标题标题。 我必须使用 apache poi 在 Excel 工作表中创建数据透视表。现在使用 java 更改行和列标题标签。

最佳答案

import java.io.FileOutputStream;
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;
public class readxlsx {  
        public static void main(String[] args) throws Exception{
                /* Read the input file that contains the data to pivot */
                FileInputStream input_document = new FileInputStream(new File("inputFile.xlsx"));    
                /* Create a POI XSSFWorkbook Object from the input file */
                XSSFWorkbook my_xlsx_workbook = new XSSFWorkbook(input_document); 
                /* Read Data to be Pivoted - we have only one worksheet */
                XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0); 
                /* Get the reference for Pivot Data */
                AreaReference a=new AreaReference("A1:C51");
                /* Find out where the Pivot Table needs to be placed */
                CellReference b=new CellReference("I5");
                /* Create Pivot Table */
                XSSFPivotTable pivotTable = sheet.createPivotTable(a,b);
                /* Add filters */
                pivotTable.addReportFilter(0);
                pivotTable.addRowLabel(1);
                pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2); 
                /* Write Pivot Table to File */
                FileOutputStream output_file = new FileOutputStream(new File("POI_XLS_Pivot_Example.xlsx")); 
                my_xlsx_workbook.write(output_file);
                input_document.close(); 
        }
}

这可能会对您的问题有所帮助。

关于java - 使用java在Excel工作表中创建数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42255424/

相关文章:

java - 在字符串数组中搜索下一个可用的空白空间,然后将空间标记为已占用

java - 极其精确的重复/连续代码执行

java - 将页脚项目添加到 Android ListView 时出现 NullPointerException

java - JTable setValueAt StackOverflowError

excel - 范围内的二维数组

java - 无法使用 apache poi 从 Excel 文件读取

java - XWPFDocument 循环替换段落

Excel:删除 ? 之后的所有内容在网址中

python - 使用Python编辑Excel工作表单元格

Java POI api在后台更新excel数据而不关闭excel?