In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Here we use the maven project and give the POM file as a reference:
Org.apache.poi
Poi
3.15
Org.apache.poi
Poi-ooxml
3.15
The general idea is to call API, create a new workbook, create a new sheet, get the number of row rows to operate on, and manipulate the contents of cell cells. It should be noted that the HSSF in Apache POI (HSSF+ XSSF) corresponds to the pre-2004 version, and the XSSF corresponds to the 2004 version of the excel file, that is, XSSF is needed at the end of the .xlsx file.
The coordinates of row,cell start at 0 instead of 1.
JSON is passed into the export Excel as data
Public class ExcelUtil {
@ SuppressWarnings ("unchecked")
/ / create excel file function
/ / src is the path of the file to be saved, json is the json data to be saved
Public static JSONObject createExcel (String src, JSONObject json) {
JSONObject result = new JSONObject (); / / is used to feedback the result of the function call
Try {
/ / create a new file
File file = new File (src)
File.createNewFile ()
OutputStream outputStream = new FileOutputStream (file); / / create a workbook
WritableWorkbook writableWorkbook = Workbook.createWorkbook (outputStream)
WritableSheet sheet = writableWorkbook.createSheet ("First sheet", 0); / / create a new page
JSONArray jsonArray = json.getJSONArray ("data"); / / get the JSONArray corresponding to data
Label label; / / Cell object
Int column = 0; / / column count
/ / add the first line of information to the page. Such as: name, age, gender
JSONObject first = jsonArray.getJSONObject (0)
Iterator iterator = first.keys (); / / get the key set of the first item
While (iterator.hasNext ()) {/ / traverse the key collection
String key = (String) iterator.next (); / / get key
Label = new Label (column++, 0, key); / / the first parameter is the column of the cell, the second parameter is the row of the cell, and the third parameter is the value
Sheet.addCell (label); / / add cells to the page
}
/ / traversing jsonArray
For (int I = 0; I < jsonArray.size (); iTunes +) {
JSONObject item = jsonArray.getJSONObject (I); / / get each item of the array
Iterator = item.keys (); / / get the key set
Column = 0 position / starting from column 0
While (iterator.hasNext ()) {
String key = iterator.next (); / / get key
String value = item.getString (key); / / get the value corresponding to key
Label = new Label (column++, (I + 1), value); / / the first parameter is the column of the cell, the second parameter is the row of the cell, and the third parameter is the value
Sheet.addCell (label); / / add cells to the page
}
}
WritableWorkbook.write (); / / add to the file
WritableWorkbook.close (); / / close the file and release resources
} catch (Exception e) {
Result.put ("result", "failed"); / / returns the result of calling this function
Result.put ("reason", e.getMessage ()); / / returns the reason for the failure of calling the function
Return result
}
Result.put ("result", "successed")
Return result
}
}
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.