json to excel java
300
2024-03-06 14:53
java
import org.apache.poi.ss.usermodel.*;
public class JsonToExcelConverter {
public static void main(String[] args) {
// 解析JSON数据
String jsonData = "{\"name\": \"Alice\", \"age\": 30}";
// 创建Excel文件
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 创建表头
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("Name");
headerRow.createCell(1).setCellValue("Age");
// 写入数据
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("Alice");
dataRow.createCell(1).setCellValue(30);
// 保存文件
try (FileOutputStream outputStream = new FileOutputStream("output.xlsx")) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
顶一下
(0)
踩一下
(0)