Java for data analysis in Excel

Abstract: This article was originally created and published by the technical team of Grape City in the Nuggets. Please indicate the source of the reprint: Grape City official website , Grape City provides developers with professional development tools, solutions and services to empower developers.

Some time ago, Taobao launched a module called "Taobao Life", where you can view the consumption records from the time you registered your Taobao account. After thinking about it carefully, now WeChat and Taobao seem to like to record users' payment and consumption in this way. function. However, this function of displaying consumption records does make people feel a lot more convenient. In this way, everyone can check the previous consumption records anytime and anywhere. Sometimes it is necessary to check the account, and you can see where the money has gone by flipping through the phone, and the flow of every money can be seen very clearly. Since this thing is so easy to use, can I also develop a similar analysis tool, so that I can use it to record every bit of life. Due to the nature of my work, I am more familiar with Excel. The first thing I thought of was whether it is possible to use a table visualization tool to realize this function.

Just do what you say, first search the Internet to find some styles of visualization tools in Excel, and see that charts (column charts, bar charts, etc.) and pivot charts are more popular in Excel. Because charts are a tool that is commonly used, driven by curiosity, Baidu took a look at "How to use code to make a pivot chart in a table"! After browsing, I found that there are many languages ​​​​that can be used. Implementation (Python, Java, JavaScript, .net, etc.). Since I am more familiar with the Java language, I continued Baidu's "How to use Java to make a pivot table in Excel". Found that this can be achieved using the Apache POI library:

 
 

java

copy code

import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class PivotTableExample { public static void main(String[] args) throws IOException { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("Data"); // 输入数据 Row headingRow = sheet.createRow(0); headingRow.createCell(0).setCellValue("Category"); headingRow.createCell(1).setCellValue("Value"); Row dataRow1 = sheet.createRow(1); dataRow1.createCell(0).setCellValue("A"); dataRow1.createCell(1).setCellValue(10); Row dataRow2 = sheet.createRow(2); dataRow2.createCell(0).setCellValue("B"); dataRow2.createCell(1).setCellValue(20); Row dataRow3 = sheet.createRow(3); dataRow3.createCell(0).setCellValue("A"); dataRow3.createCell(1).setCellValue(15); // 创建数据透视表 XSSFPivotTable pivotTable = ((XSSFSheet) sheet).createPivotTable(new AreaReference("A1:B3", SpreadsheetVersion.EXCEL2007), new CellReference("D5")); // 设置行标签 pivotTable.addRowLabel(0); // 设置值字段 pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1, "Sum of Value"); // 保存Excel文件 FileOutputStream fileOut = new FileOutputStream("pivotTable.xlsx"); workbook.write(fileOut); fileOut.close(); System.*out*.println("数据透视表已创建并保存到文件!"); } }

In addition to using the Apache POI library, I also found a commercial software GcExcel. Since I don’t know much about this thing, I simply chatted about GcExcel.

After checking, I found that, similar to the Apache POI library, GcExcel is also a Java-based table operation library, so with a curious attitude, I searched "Java implements GcExcel pivot table" on Baidu. I found a GcExcel study guide , which contains some source code and code explanations. According to the introductory tutorial inside, I wrote a small demo that implements the pivot table (because the complete code is too long, only part of it is intercepted):

Children's shoes who want the complete code can be downloaded from Gitee or Github :

 
 

java

copy code

public class Main { public static void main(String[] args){ Workbook workbook = new Workbook(); //创建一个WorkSheet的对象 IWorksheet worksheet = workbook.getWorksheets().get(0); //-----------------------------设置数据值------------------------------ worksheet.getRange("B3:C7").setValue(new Object[][]{ {"ITEM", "AMOUNT"}, {"Income 1", 2500}, {"Income 2", 1000}, {"Income 3", 250}, {"Other", 250}, }); worksheet.getRange("B10:C23").setValue(new Object[][]{ {"ITEM", "AMOUNT"}, {"Rent/mortgage", 800}, {"Electric", 120}, {"Gas", 50}, {"Cell phone", 45}, {"Groceries", 500}, {"Car payment", 273}, {"Auto expenses", 120}, {"Student loans", 50}, {"Credit cards", 100}, {"Auto Insurance", 78}, {"Personal care", 50}, {"Entertainment", 100}, {"Miscellaneous", 50}, }); //合并单元格 worksheet.getRange("B2:C2").merge(); worksheet.getRange("B2").setValue("MONTHLY INCOME"); worksheet.getRange("B9:C9").merge(); worksheet.getRange("B9").setValue("MONTHLY EXPENSES"); worksheet.getRange("E2:G2").merge(); worksheet.getRange("E2").setValue("PERCENTAGE OF INCOME SPENT"); worksheet.getRange("E5:G5").merge(); worksheet.getRange("E5").setValue("SUMMARY"); worksheet.getRange("E3:F3").merge(); worksheet.getRange("E9").setValue("BALANCE"); worksheet.getRange("E6").setValue("Total Monthly Income"); worksheet.getRange("E7").setValue("Total Monthly Expenses"); //--------------------------------设置形状-------------------------------- IShape shape = worksheet.getShapes().addChart(ChartType.*ColumnClustered*, 339, 247, 316.5, 346); shape.getChart().getChartArea().getFormat().getLine().setTransparency(1); shape.getChart().getColumnGroups().get(0).setOverlap(0); shape.getChart().getColumnGroups().get(0).setGapWidth(37); IAxis category_axis = shape.getChart().getAxes().item(AxisType.*Category*); category_axis.getFormat().getLine().getColor().setRGB(Color.*GetBlack*()); category_axis.getTickLabels().getFont().setSize(11); category_axis.getTickLabels().getFont().getColor().setRGB(Color.*GetBlack*()); IAxis series_axis = shape.getChart().getAxes().item(AxisType.*Value*); series_axis.getFormat().getLine().setWeight(1); series_axis.getFormat().getLine().getColor().setRGB(Color.*GetBlack*()); series_axis.getTickLabels().setNumberFormat("\$\#\#\#0"); series_axis.getTickLabels().getFont().setSize(11); series_axis.getTickLabels().getFont().getColor().setRGB(Color.*GetBlack*()); ISeries chartSeries = shape.getChart().getSeriesCollection().newSeries(); chartSeries.setFormula("=SERIES(\"Simple Budget\",{"Income\","Expenses\"},'Sheet1'!$G$6:$G$7,1)"); chartSeries.getPoints().get(0).getFormat().getFill().getColor().setRGB(Color.*FromArgb*(176, 21, 19)); chartSeries.getPoints().get(1).getFormat().getFill().getColor().setRGB(Color.*FromArgb*(234, 99, 18)); chartSeries.getDataLabels().getFont().setSize(11); chartSeries.getDataLabels().getFont().getColor().setRGB(Color.*GetBlack*()); chartSeries.getDataLabels().setShowValue(true); chartSeries.getDataLabels().setPosition(DataLabelPosition.*OutsideEnd*); workbook.save("tutorial.xlsx"); } }

Final Excel style:

Through the above experiments, both Apache POI and GcExcel can be used to implement pivot tables in Excel, and you can choose the appropriate method according to the needs of your project or project.

Extension link:

How to make your system also have the function of "pivot table"

PivotTables are live! How to realize this powerful data analysis function in the pure front end

Pure front-end Excel pivot table to realize the application of profit and loss statement

Guess you like

Origin blog.csdn.net/BASK2312/article/details/131662277