将以下数据用Java代码以excel导出 "食品产业": { "2019": 0, "2020": 0, "2021": 1, "2022": 2, "2023": 1 }, "液压科技产业": { "2019": 0, "2020": 0, "2021": 0, "2022": 1, "2023": 0 }, "智能装备产业": { "2019": 1, "2020": 1, "2021": 2, "2022": 0, "2023": 2 }, "其他产业": { "2019": 0, "2020": 0, "2021": 0, "2022": 0, "2023": 0 }
时间: 2024-01-11 17:03:33 浏览: 25
可以使用相同的Apache POI库,根据数据格式,导出Excel表格。以下是一个示例代码:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelExporter {
public static void main(String[] args) {
// 创建一个新的工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 创建一个新的工作表
workbook.createSheet("数据");
// 获取工作表中的第一行
Row row = workbook.getSheet("数据").createRow(0);
// 在第一行中创建单元格并写入数据
Cell cell1 = row.createCell(0);
cell1.setCellValue("行业");
Cell cell2 = row.createCell(1);
cell2.setCellValue("2019");
Cell cell3 = row.createCell(2);
cell3.setCellValue("2020");
Cell cell4 = row.createCell(3);
cell4.setCellValue("2021");
Cell cell5 = row.createCell(4);
cell5.setCellValue("2022");
Cell cell6 = row.createCell(5);
cell6.setCellValue("2023");
// 在第二行中创建单元格并写入数据
Row row2 = workbook.getSheet("数据").createRow(1);
Cell cell7 = row2.createCell(0);
cell7.setCellValue("食品产业");
Cell cell8 = row2.createCell(1);
cell8.setCellValue(0);
Cell cell9 = row2.createCell(2);
cell9.setCellValue(0);
Cell cell10 = row2.createCell(3);
cell10.setCellValue(1);
Cell cell11 = row2.createCell(4);
cell11.setCellValue(2);
Cell cell12 = row2.createCell(5);
cell12.setCellValue(1);
// 在第三行中创建单元格并写入数据
Row row3 = workbook.getSheet("数据").createRow(2);
Cell cell13 = row3.createCell(0);
cell13.setCellValue("液压科技产业");
Cell cell14 = row3.createCell(1);
cell14.setCellValue(0);
Cell cell15 = row3.createCell(2);
cell15.setCellValue(0);
Cell cell16 = row3.createCell(3);
cell16.setCellValue(0);
Cell cell17 = row3.createCell(4);
cell17.setCellValue(1);
Cell cell18 = row3.createCell(5);
cell18.setCellValue(0);
// 在第四行中创建单元格并写入数据
Row row4 = workbook.getSheet("数据").createRow(3);
Cell cell19 = row4.createCell(0);
cell19.setCellValue("智能装备产业");
Cell cell20 = row4.createCell(1);
cell20.setCellValue(1);
Cell cell21 = row4.createCell(2);
cell21.setCellValue(1);
Cell cell22 = row4.createCell(3);
cell22.setCellValue(2);
Cell cell23 = row4.createCell(4);
cell23.setCellValue(0);
Cell cell24 = row4.createCell(5);
cell24.setCellValue(2);
// 在第五行中创建单元格并写入数据
Row row5 = workbook.getSheet("数据").createRow(4);
Cell cell25 = row5.createCell(0);
cell25.setCellValue("其他产业");
Cell cell26 = row5.createCell(1);
cell26.setCellValue(0);
Cell cell27 = row5.createCell(2);
cell27.setCellValue(0);
Cell cell28 = row5.createCell(3);
cell28.setCellValue(0);
Cell cell29 = row5.createCell(4);
cell29.setCellValue(0);
Cell cell30 = row5.createCell(5);
cell30.setCellValue(0);
// 保存工作簿
try (FileOutputStream outputStream = new FileOutputStream("data.xlsx")) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这段代码将创建一个名为“数据”的工作表,并将数据写入第一行和第二行。你可以根据你的数据格式来修改代码。最后,使用`FileOutputStream`将工作簿保存到文件中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)