色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

java excel to json

Java Excel到JSON是根據(jù)Excel文件創(chuàng)建JSON對(duì)象的操作。它是Java開發(fā)中經(jīng)常用到的一種技術(shù)。新手可能對(duì)如何將Excel轉(zhuǎn)換為Json對(duì)象感到有些困惑,但是這其實(shí)并不難,只需要使用一些現(xiàn)成的Java庫(kù)即可完成操作。

以下代碼演示了如何使用Java Excel到JSON庫(kù)將Excel文件轉(zhuǎn)換成JSON對(duì)象:

// 導(dǎo)入您的庫(kù)
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import org.apache.poi.ss.usermodel.*;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.util.Iterator;
public class ExcelToJson {
public static void main(String[] args) throws Exception {
//讀取Excel文件
FileInputStream inputStream = new FileInputStream(new File("path/to/excel/file.xlsx"));
Workbook workbook = WorkbookFactory.create(inputStream);
// 將Excel轉(zhuǎn)換成JSON
JSONArray jsonArray = new JSONArray();
for (int i = 0; i< workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
Iteratoriterator = sheet.iterator();
JSONObject jsonObject;
// 獲取Excel表頭
Row headerRow = iterator.next();
String[] headers = new String[headerRow.getPhysicalNumberOfCells()];
int index = 0;
for(IteratorcellIterator = headerRow.cellIterator(); cellIterator.hasNext();) {
Cell cell = cellIterator.next();
headers[index++] = cell.getStringCellValue();
}
// 遍歷Excel行
while (iterator.hasNext()) {
jsonObject = new JSONObject();
Row row = iterator.next();
for (int j = 0; j< headers.length; j++) {
Cell cell = row.getCell(j);
switch (cell.getCellTypeEnum()) {
case STRING:
jsonObject.put(headers[j], cell.getStringCellValue().trim());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
jsonObject.put(headers[j], cell.getDateCellValue().toString().trim());
} else {
jsonObject.put(headers[j], Double.toString(cell.getNumericCellValue()));
}
break;
case BOOLEAN:
jsonObject.put(headers[j], cell.getBooleanCellValue());
break;
case FORMULA:
jsonObject.put(headers[j], cell.getCellFormula());
break;
default:
jsonObject.put(headers[j], JSONObject.NULL);
break;
}
}
jsonArray.put(jsonObject);
}
}
// 將JSON寫入文件
FileWriter fileWriter = new FileWriter(new File("path/to/json/file.json"));
fileWriter.write(jsonArray.toString(4));
fileWriter.flush();
fileWriter.close();
}
}