java导出Excel文档数据表通用版

//控制层
// 清除buffer缓存
response.reset();
// 指定下载的文件名
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmss”);
String time = sdf.format(date);
time=“导出文件字典信息Excel-”+time;
response.setHeader(“Content-Disposition”, “attachment;filename=”+
new String(time.getBytes(“utf-8”),“iso-8859-1”)+ “.xlsx”);
response.setContentType(“application/vnd.ms-excel;charset=UTF-8”);
response.setHeader(“Pragma”, “no-cache”);
response.setHeader(“Cache-Control”, “no-cache”);
response.setDateHeader(“Expires”, 0);
XSSFWorkbook workbook = null;
List list = iAbsFiledictService.SelectAllFiledictImportExcel(core_id,file_source);
workbook = ExcelExport.AbsFiledictExportContacts(list);
OutputStream output;
output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
workbook.write(bufferedOutPut);
bufferedOutPut.close();
return null;

生成Excel文档工具类
public class ExcelExport {

//文件字典列表
public static XSSFWorkbook AbsFiledictExportContacts(List<AbsFiledict> list) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
    XSSFWorkbook xssfWorkbook = null;
    String sheetName = "文件字典信息表";
    xssfWorkbook = AbsFiledictExportContactsFile(list, sheetName);
    return xssfWorkbook;

}

//已认证数据生成EXCEL
public static XSSFWorkbook AbsFiledictExportContactsFile(List<AbsFiledict> list, String sheetName) throws IllegalArgumentException {
    // 创建新的Excel工作簿
    XSSFWorkbook workbook = new XSSFWorkbook();
    // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
    XSSFSheet sheet = workbook.createSheet(sheetName);
    XSSFRow row = sheet.createRow(0);

    CellStyle style = workbook.createCellStyle();
    XSSFFont font = workbook.createFont();
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//表头粗体显示
    style.setFont(font);

    XSSFCell cell = row.createCell(0);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 0, (short) 5000);
    cell.setCellValue("文件字典ID");
    cell = row.createCell(1);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 1, (short) 5000);
    cell.setCellValue("核心企业ID");
    cell = row.createCell(2);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 2, (short) 5000);
    cell.setCellValue("文件名称");
    cell = row.createCell(3);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 3, (short) 5000);
    cell.setCellValue("文件编号");
    cell = row.createCell(4);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 4, (short) 5000);
    cell.setCellValue("文件来源");
    cell = row.createCell(5);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 5, (short) 5000);
    cell.setCellValue("文件类型");
    cell = row.createCell(6);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 6, (short) 5000);
    cell.setCellValue("文件要求");
    cell = row.createCell(7);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 7, (short) 5000);
    cell.setCellValue("盖章要求");
    cell = row.createCell(8);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 8, (short) 5000);
    cell.setCellValue("文件份数");
    cell = row.createCell(9);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 9, (short) 5000);
    cell.setCellValue("文件模板");
    cell = row.createCell(10);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 10, (short) 5000);
    cell.setCellValue("审核要点");
    cell = row.createCell(11);
    cell.setCellStyle(style);sheet.setColumnWidth((short) 11, (short) 5000);
    cell.setCellValue("文件备注");

    //放入数据及列自适应宽
    for (int i = 0; i < list.size(); i++) {
        row = sheet.createRow(i + 1);
        row.createCell(0).setCellValue(list.get(i).getFiledictId());
        row.createCell(1).setCellValue(list.get(i).getCore_enterp_id());
        row.createCell(2).setCellValue(list.get(i).getFiledictName());
        row.createCell(3).setCellValue(list.get(i).getFiledictNo());
        row.createCell(4).setCellValue(list.get(i).getFiledictSource());
        row.createCell(5).setCellValue(list.get(i).getFiledictType());
        row.createCell(6).setCellValue(list.get(i).getFiledictRequire());
        row.createCell(7).setCellValue(list.get(i).getFiledictSeal());
        row.createCell(8).setCellValue(list.get(i).getFiledictNumber());
        row.createCell(9).setCellValue(list.get(i).getFiledictUrl());
        row.createCell(10).setCellValue(list.get(i).getFiledictAuditpoint());
        row.createCell(11).setCellValue(list.get(i).getFiledictDesc());
    }
    return workbook;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java导出Excel数据,你可以使用Apache POI库。以下是一个简单的示例代码,演示如何导出数据到Excel文件: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExporter { public static void main(String[] args) { // 创建工作簿 try (Workbook workbook = new XSSFWorkbook()) { // 创建工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建数据行 Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("姓名"); headerRow.createCell(1).setCellValue("年龄"); headerRow.createCell(2).setCellValue("性别"); Row dataRow = sheet.createRow(1); dataRow.createCell(0).setCellValue("张三"); dataRow.createCell(1).setCellValue(25); dataRow.createCell(2).setCellValue("男"); // 导出到文件 try (FileOutputStream outputStream = new FileOutputStream("data.xlsx")) { workbook.write(outputStream); } } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码使用了Apache POI库创建了一个Excel工作簿和一个工作表,并在第一个数据行中填充了一些数据。最后,使用`FileOutputStream`将工作簿写入到文件中。 请注意,上述代码使用的是XSSFWorkbook类,它是针对Excel 2007及以上版本的XLSX文件格式。如果需要支持Excel 2003及以下版本的XLS文件格式,可以使用HSSFWorkbook类。 希望对你有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值