Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of generating and exporting excel files for Java tree structure data

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is the method of generating and exporting excel files for Java tree structure data". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is tree structure data?

Effect.

Usage String jsonStr = "{\" name\ ":\" aaa\ ",\" children\ ": [{\" name\ ":\" bbb\ ",\" children\ ": [{\" name\ ":\" eee\ "}, {\" name\ ":\" fff\ ",\" children\ ": [{\" name\ ":\" iii\ "}, {\" name\ ":\" jjj\ ",\" children\ ": [{\" name\ ":\" qqq\ "} {\ "name\":\ "ttt\"}]}, {\ "name\":\ "www\"]}, {\ "name\":\ "ccc\",\ "children\": [{\ "name\":\ "ggg\"}, {\ "name\":\ "hhh\",\ "children\": [{\ "name\":\ "kkk\",\ "children\": [{\ "name\":\ "ttt\"} {\ "name\":\ "mmm\"}]}, {\ "name\":\ "uuu\"}]}, {\ "name\":\ "ooo\"}]}, {\ "name\":\ "ddd\",\ "children\": [{\ "name\":\ "ggg\"}, {\ "name\":\ "hhh\",\ "children\": [{\ "name\":\ "kkk\" {\ "name\":\ "uuu\"]} " Map tree = JSONObject.parseObject (jsonStr, Map.class); tree2Excel (tree, "E:\\" + System.currentTimeMillis () + ".xls", "name", "children"); Source package pers.xxx.demo.tree2excel; import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.Closeable;import java.io.File;import java.io.FileOutputStream;import java.io.IOException Import java.util.List;import java.util.Map; / * tree structure data export excel tool *

* Created by lzy on 2021-2-24 14:09 * / @ SuppressWarnings ("ALL") public class Tree2ExcelUtil {/ * generate excel file * * @ param tree tree data * @ param filePath file path * @ return * / public static boolean tree2Excel (Map tree, String filePath) {return tree2Excel (tree, filePath, null, null) } / * Tree data generation excel file * * @ param tree tree data * @ param filePath file path * @ param lableName tag Key name * @ param childrenName child node Key name * @ return * / public static boolean tree2Excel (Map tree, String filePath, String lableName String childrenName) {if (isBlank (filePath)) {System.err.println ("File name cannot be empty") Return false;} try {doSame (tree, lableName, childrenName); createExcel (filePath, tree); return true;} catch (IOException e) {e.printStackTrace ();} return false } / * Tree structure data generates Workbook object * * @ param tree Tree data * @ param fileSuf File suffix, xls/xlsx * @ return * / public static Workbook tree2Worbook (Map tree, String fileSuf) {return tree2Worbook (tree, fileSuf, null, null) } / * Tree structure data generates Workbook object * * @ param tree Tree data * @ param fileSuf File suffix Xls/xlsx * @ param lableName tag Key name * @ param childrenName child node Key name * @ return * / public static Workbook tree2Worbook (Map tree, String fileSuf, String lableName, String childrenName) {if (isBlank (fileSuf)) {System.err.println ("File suffix must be specified") Return null;} try {doSame (tree, lableName, childrenName); return procesData (tree, fileSuf);} catch (Exception e) {e.printStackTrace ();} return null;} / / specific implementation / * * identify maximum column * / private static int maxCol = 0 Private static String lableName = "lable"; private static String childrenName = "children"; private static final String COL = "col"; private static final String ROW = "row"; private static final String ROW_OFT = "rowOft"; private static final String ROW_SIZE = "rowSize"; private static void doSame (Map tree, String lableName, String childrenName) {if (! isBlank (lableName)) {Tree2ExcelUtil.lableName = lableName } if (! isBlank (childrenName)) {Tree2ExcelUtil.childrenName = childrenName;} coreAlgoCol (tree, 1); coreAlgoRow (tree) } / * main algorithm, calculate the coordinates of the column, calculate the row occupied by each node * * @ param tree data * @ param col incrementing column * @ param trees pass the high level down to calculate the incremented row height * / private static void coreAlgoCol (Map tree, int col, Map... Trees) {tree.put (COL, col); Object childrenObj = tree.get (childrenName); if (childrenObj! = null) {List children = (List) childrenObj; if (children.size () > 0) {int size = children.size () * 2-1; tree.put (ROW_SIZE, size) Int len = trees! = null? Trees.length + 1: 1; Map [] arrData = new Map [len]; if (trees! = null & & trees.length > 0) {for (int I = 0; I)

< trees.length; i++) { Map tree1 = trees[i]; tree1.put(ROW_SIZE, toInt(tree1.get(ROW_SIZE), 1) + size - 1); arrData[i] = tree1; } } arrData[len - 1] = tree; for (Map tree1 : children) { int newCol = col + 1; if (newCol >

MaxCol) {maxCol = newCol;} coreAlgoCol (tree1, newCol, arrData) }} / * main algorithm to calculate the coordinates of the row * * @ param tree * / private static void coreAlgoRow (Map tree) {if (toInt (tree.get (ROW)) = = 0) {tree.put (ROW, Math.round (toInt (tree.get (ROW_SIZE), 1) / 2.0f)) } Object childrenObj = tree.get (childrenName); if (childrenObj! = null) {List children = (List) childrenObj; if (children.size () > 0) {int tempOft = toInt (tree.get (ROW_OFT)); for (Map tree1: children) {int rowSize = toInt (tree1.get (ROW_SIZE), 1) Tree1.put (ROW_OFT, tempOft); tree1.put (ROW, tempOft + Math.round (rowSize / 2.0f)); tempOft + = rowSize + 1; coreAlgoRow (tree1) }} / * create excel file * * @ param filePath file path, specific path to file name * @ param tree data * @ throws IOException * / private static void createExcel (String filePath, Map tree) throws IOException {File file = new File (filePath); boolean bfile = file.createNewFile () / / copy the template to the new file if (bfile) {Workbook wk = procesData (tree, filePath); if (wk! = null) {FileOutputStream fos = null; try {fos = new FileOutputStream (file); wk.write (fos); fos.flush () } finally {closeStream (fos); wk.close () Deal with excel data * * @ param tree data * @ return worksheet object * / private static Workbook procesData (Map tree, String fileName) {Workbook wk = null; if (fileName.endsWith ("xls")) {wk = new HSSFWorkbook () } if (fileName.endsWith ("xlsx")) {wk = new XSSFWorkbook ();} if (wk = = null) {System.err.println ("incorrect file name"); return null;} / / create a sheet page Sheet sheet = wk.createSheet ("Sheet1"); int colSize = maxCol * 2 + 2 Int rowSize = toInt (tree.get (ROW_SIZE), 1); for (int I = 0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report