1个回答
test
2024-06-24
你可以使用java Excel 相关的第三方组件实现引入Maven依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
Java将数据写入Excel,把这个方法封装了WriteToExcel
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class WriteToExcel {
private static XSSFWorkbook workbook;
private static XSSFSheet sheet;
private static XSSFRow row;
private static XSSFCell cell;
private static File file;
//创建sheet页
public static void setSheet(String sheetName) {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(sheetName);
}
//创建表头
public static void createHead(List<String> headList) {
//创建表头,也就是第一行
row = sheet.createRow(0);
for (int i = 0; i < headList.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(headList.get(i));
}
}
//创建表内容
public static void createContent(List<List<String>> contentList) {
//创建表内容,从第二行开始
for (int i = 0; i < contentList.size(); i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < contentList.get(i).size(); j++) {
row.createCell(j).setCellValue(contentList.get(i).get(j));
}
}
}
//写入文件
public static void writeToFile(String filePath){
file = new File(filePath);
//将文件保存到指定的位置
try {
workbook.write(new FileOutputStream(file));
System.out.println("写入成功");
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 内容测试数据
protected static List<List<String>> getContent() {
List<List<String>> contentList = new ArrayList<>();
List<String> content1 = new ArrayList<>();
content1.add("张三");
content1.add("18");
List<String> content2 = new ArrayList<>();
content2.add("李四");
content2.add("20");
contentList.add(content1);
contentList.add(content2);
return contentList;
}
public static void main(String[] args) {
//表头测试数据
List<String> headList = new ArrayList<>();
headList.add("昵称");
headList.add("年龄");
List<List<String>> contentList = getContent();//内容测试数据
setSheet("WorkSheet"); //创建sheet页
createHead(headList); //设置表头
createContent(contentList); //设置内容
writeToFile("D://work.xls"); //写入文件
}
}
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容