java导出类_Java导出excel工具类

导读热词

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.List;

import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportExcel {

private ExportExcel() {

super();

}

public static void exportExcel(List list,Map map,String[] titles) throws IOException {

// 创建Excel文档

HSSFWorkbook hwb = new HSSFWorkbook();

// sheet 对应一个工作页

HSSFSheet sheet = hwb.createSheet("exportReport");

int colNum = titles.length;

// 创建第一行

HSSFRow firstrow = sheet.createRow(0);

HSSFCell[] firstcell = new HSSFCell[colNum];

for (int col = 0; col < colNum; col++) {

firstcell[col] = firstrow.createCell(col);

firstcell[col].setCellValue(new HSSFRichTextString(titles[col]));

}

// 插入记录

int rowNum = map.size();

for (int i = 0; i < rowNum; i++) {

// 从第二行开始

HSSFRow row = sheet.createRow(i + 1);

// 插入list中的字段

for (int col = 0; col < colNum - 2; col++) {

HSSFCell cell = row.createCell(col);

cell.setCellValue(list.get(col).toString());

}

// 插入月份或日期

row.createCell(colNum - 2).setCellValue(i + 1);

// 插入总量

row.createCell(colNum - 1).setCellValue(map.get(i + 1));

}

String fileName = titles[1].substring(0,2);

if (colNum == 4) {

fileName += list.get(0) + "_" + list.get(1) + "年_年度报表";

} else if (colNum == 5) {

fileName += list.get(0) + "_" + list.get(1) + "年" + list.get(2)

+ "月_月度报表";

}

// 创建文件输出流,准备输出电子表格

OutputStream out = new FileOutputStream("../webapps/UsedMallMinaServer/files/"

+ fileName + ".xls");

hwb.write(out);

out.close();

}

}

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

总结

以上是编程之家为你收集整理的Java导出excel工具类全部内容,希望文章能够帮你解决Java导出excel工具类所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java导出Excel工具类示例,使用Apache POI库: ``` import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtil { public static void exportDataToExcel(List<MyData> dataList, String filePath) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Data Sheet"); createHeaderRow(sheet); int rowCount = 1; for (MyData data : dataList) { Row row = sheet.createRow(rowCount++); writeDataToRow(data, row); } autosizeColumns(sheet); try (FileOutputStream outputStream = new FileOutputStream(filePath)) { workbook.write(outputStream); } } private static void createHeaderRow(Sheet sheet) { Row headerRow = sheet.createRow(0); CellStyle headerCellStyle = sheet.getWorkbook().createCellStyle(); headerCellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); headerCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); String[] headers = {"ID", "Name", "Age", "Gender"}; for (int i = 0; i < headers.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); cell.setCellStyle(headerCellStyle); } } private static void writeDataToRow(MyData data, Row row) { Cell idCell = row.createCell(0); idCell.setCellValue(data.getId()); Cell nameCell = row.createCell(1); nameCell.setCellValue(data.getName()); Cell ageCell = row.createCell(2); ageCell.setCellValue(data.getAge()); Cell genderCell = row.createCell(3); genderCell.setCellValue(data.getGender()); } private static void autosizeColumns(Sheet sheet) { for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) { sheet.autoSizeColumn(i); } } } ``` 在上面的代码中,我们首先创建一个XSSFWorkbook对象,然后创建一个名为“Data Sheet”的工作表。接下来,我们在工作表中创建一个标题行,并设置单元格样式。然后,我们遍历数据列表并将数据写入每一行。最后,我们自适应每列的宽度,然后将工作簿写入输出流中保存到指定路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值