导出excel数据 java
时间: 2023-05-31 18:05:56 浏览: 85
java 数据导出excel
在Java中导出Excel数据,可以使用Apache POI库。以下是一个示例代码:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
public class ExcelExporter {
public static void main(String[] args) throws IOException {
// 创建工作簿
Workbook workbook = WorkbookFactory.create(true);
// 创建工作表
Sheet sheet = workbook.createSheet("Sheet1");
// 创建表头行
Row headerRow = sheet.createRow(0);
// 添加表头单元格
Cell headerCell1 = headerRow.createCell(0);
headerCell1.setCellValue("姓名");
Cell headerCell2 = headerRow.createCell(1);
headerCell2.setCellValue("年龄");
Cell headerCell3 = headerRow.createCell(2);
headerCell3.setCellValue("性别");
// 设置表头样式
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
headerCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
headerCell1.setCellStyle(headerCellStyle);
headerCell2.setCellStyle(headerCellStyle);
headerCell3.setCellStyle(headerCellStyle);
// 添加数据行
List<Person> persons = getPersonList();
for (int i = 0; i < persons.size(); i++) {
Person person = persons.get(i);
Row dataRow = sheet.createRow(i + 1);
Cell dataCell1 = dataRow.createCell(0);
dataCell1.setCellValue(person.getName());
Cell dataCell2 = dataRow.createCell(1);
dataCell2.setCellValue(person.getAge());
Cell dataCell3 = dataRow.createCell(2);
dataCell3.setCellValue(person.getGender());
}
// 自适应列宽
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
sheet.autoSizeColumn(i);
}
// 设置数字格式
DataFormat dataFormat = workbook.createDataFormat();
CellStyle ageCellStyle = workbook.createCellStyle();
ageCellStyle.setDataFormat(dataFormat.getFormat("0"));
sheet.setDefaultColumnStyle(1, ageCellStyle);
// 保存文件
FileOutputStream fileOut = new FileOutputStream("persons.xlsx");
workbook.write(fileOut);
fileOut.close();
// 关闭工作簿
workbook.close();
}
private static List<Person> getPersonList() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("张三", 20, "男"));
persons.add(new Person("李四", 25, "女"));
persons.add(new Person("王五", 30, "男"));
return persons;
}
private static class Person {
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
}
```
以上代码将会创建一个Excel文件,其中包含一个名为“Sheet1”的工作表,表头为“姓名”、“年龄”、“性别”,数据为三个人的姓名、年龄和性别信息。在代码中,我们使用了Apache POI库来创建和编辑Excel文件。首先,我们创建一个工作簿(Workbook)对象,然后创建一个工作表(Sheet)对象,并在工作表中添加表头和数据行。我们还设置了表头和数字单元格的样式,并使用自适应列宽和数字格式。最后,我们使用文件输出流将工作簿写入到Excel文件中,并关闭工作簿。
阅读全文