java excel动态导出excel_java后台动态生成导出excel

p

ublic void export(List wechatUserList, HttpServletResponse response) throws IOException, URISyntaxException {

File newFile = createNewFile(getClass().getClassLoader().getResource("../../resources/excel/wechatUserInfoTemplate.xls").toURI().getPath(), new URI("../../resources/xls").getPath());//防止路径中的空格转义为%20

InputStream is = null;

HSSFWorkbook workbook = null;

HSSFSheet sheet = null;

try {

is = new FileInputStream(newFile);// 将excel文件转为输入流

workbook = new HSSFWorkbook(is);// 创建个workbook,

sheet = workbook.getSheetAt(0);

} catch (Exception e1) {

e1.printStackTrace();

}

if (sheet != null) {

try {

// 写数据

FileOutputStream fos = new FileOutputStream(newFile);

HSSFRow row = sheet.createRow(1);

HSSFCell cell = row.createCell(0);

HSSFCellStyle style = workbook.createCellStyle();

style.setAlignment(CellStyle.ALIGN_CENTER);

style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

style.setWrapText(true);

int i = 1;

for (WechatUser wechatUser : wechatUserList) {

List childrenList = wechatUser.getChildrenList();

int size = childrenList.size();

row = sheet.getRow(i);

if (row == null) {

row = sheet.createRow(i);

}

cell = getCell(row,0,cell);

cell.setCellStyle(style);

cell.setCellValue(wechatUser.getNickname());

cell = getCell(row,1,cell);

cell.setCellStyle(style);

cell.setCellValue(wechatUser.getName());

cell = getCell(row,2,cell);

cell.setCellStyle(style);

cell.setCellValue(wechatUser.getPhone());

cell = getCell(row,3,cell);

cell.setCellStyle(style);

cell.setCellValue((null == wechatUser.getSex() ? "" :

(0 == wechatUser.getSex() ? "男" : "女")));

cell = getCell(row,4,cell);

cell.setCellStyle(style);

cell.setCellValue(wechatUser.getGrade().getName());

cell = getCell(row,5,cell);

cell.setCellStyle(style);

cell.setCellValue(wechatUser.getDefaultAddress());

if (1 <= size) {

cell = getCell(row,6,cell);

cell.setCellStyle(style);

cell.setCellValue(childrenList.get(0).getName());

cell = getCell(row,7,cell);

cell.setCellStyle(style);

cell.setCellValue((null == childrenList.get(0).getGender() ? "" :

(0 == childrenList.get(0).getGender() ? "男" : "女")));

cell = getCell(row,8,cell);

cell.setCellStyle(style);

cell.setCellValue(

(null == childrenList.get(0).getBirthDay() ? "" :

DateUtil.dateToStr(

childrenList.get(0).getBirthDay(),

"yyyy-MM-dd")));

}

if (2 == size) {

cell = getCell(row,9,cell);

cell.setCellStyle(style);

cell.setCellValue(childrenList.get(1).getName());

cell = getCell(row,10,cell);

cell.setCellStyle(style);

cell.setCellValue((null == childrenList.get(1).getGender() ? "" :

(0 == childrenList.get(1).getGender() ? "男" : "女")));

cell = getCell(row,11,cell);

cell.setCellStyle(style);

cell.setCellValue((null == childrenList.get(1).getBirthDay() ? "" :

DateUtil.dateToStr(

childrenList.get(1).getBirthDay(),

"yyyy-MM-dd")));

}

i++;

}

//内容

workbook.write(fos);

fos.flush();

fos.close();

// 下载

InputStream fis = new BufferedInputStream(new FileInputStream(

newFile));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

response.reset();

response.setContentType("text/html;charset=UTF-8");

OutputStream toClient = new BufferedOutputStream(

response.getOutputStream());

response.setContentType("application/x-msdownload");

String fileName = URLEncoder.encode(

"会员信息" + ".xls",

"UTF-8");

fileName = new String(fileName.getBytes(), "ISO8859-1");

response.addHeader("Content-Disposition",

"attachment;filename=" + fileName);

response.addHeader("Content-Length", "" + newFile.length());

toClient.write(buffer);

toClient.flush();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (null != is) {

is.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

// 删除创建的新文件

this.deleteFile(newFile);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以给你提供一个简单的Java后台excel导出的实现方式: 1. 首先,利用Java的IO流功能,使用HSSFWorkbook或者XSSFWorkbook类来创建excel文件。 2. 然后,使用HSSFSheet或XSSFSheet类创建工作表,并添加表头,创建行和单元格。 3. 最后,使用FileOutputStream类将excel文件输出到本地磁盘。 ### 回答2: 在Java后台实现Excel导出功能,可以利用Apache POI库来实现。以下是一个简单的示例代码。 首先,需要在项目中引入Apache POI的依赖,可以在pom.xml文件中添加以下代码: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 接下来,创建一个ExcelUtil工具类,用于创建和导出Excel文件: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; public class ExcelUtil { public static void exportToExcel(List<List<Object>> data, String filePath) { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); int rowNum = 0; for (List<Object> rowData : data) { Row row = sheet.createRow(rowNum++); int colNum = 0; for (Object field : rowData) { Cell cell = row.createCell(colNum++); if (field instanceof String) { cell.setCellValue((String) field); } else if (field instanceof Integer) { cell.setCellValue((Integer) field); } // 添加其他类型的数据处理 // 设置单元格样式 CellStyle style = workbook.createCellStyle(); // 设置样式属性,如字体、颜色等 cell.setCellStyle(style); } } try (FileOutputStream outputStream = new FileOutputStream(filePath)) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } } } ``` 然后,在后台代码中调用ExcelUtil.exportToExcel()方法,传入要导出的数据和文件路径即可完成导出操作: ```java import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { // 模拟数据 List<List<Object>> data = new ArrayList<>(); for (int i = 1; i <= 10; i++) { List<Object> rowData = new ArrayList<>(); rowData.add("姓名" + i); rowData.add(i); // 添加其他数据字段 data.add(rowData); } String filePath = "output.xlsx"; ExcelUtil.exportToExcel(data, filePath); System.out.println("Excel导出成功,文件路径:" + filePath); } } ``` 以上代码实现了一个简单的Excel导出功能。你可以根据自己的需求修改ExcelUtil类中的导出格式和样式,或者在Main类中替换成真实的数据。该方法可以应用于后台服务的Excel导出需求。 ### 回答3: 要编写一个Java后台Excel导出功能,可以使用Apache POI库来处理Excel文件。 首先,需要导入Apache POI库的jar包。可以从官方网站(https://poi.apache.org/)下载最新版本的POI库,并将相关的jar包导入到Java项目中。 然后,需要创建一个导出Excel的方法。可以通过以下步骤来实现: 1. 创建一个Workbook对象,用于表示Excel文件。 ``` Workbook workbook = new XSSFWorkbook(); ``` 2. 创建一个Sheet对象,用于表示Excel文件中的一个工作表。 ``` Sheet sheet = workbook.createSheet("Sheet1"); ``` 3. 创建行和列,并设置单元格的值。 ``` Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("姓名"); ``` 4. 根据实际数据,使用循环创建多行多列,并设置单元格的值。 5. 最后,将Workbook对象写入到输出流中,生成Excel文件。 ``` FileOutputStream outputStream = new FileOutputStream("output.xlsx"); workbook.write(outputStream); outputStream.close(); ``` 以上就是一个简单的Java后台Excel导出的实现过程。可以根据实际需求进行扩展和优化,例如设置单元格样式、合并单元格等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值