java excel工程_Java项目导出excel

项目框架:SpringMVC Spring Mybatis

权限框架:Shiro

一、创建对应的formbean 不是model,里面属性为需要导出的属性,这里我只导出编码和名称

public class StoreBillInfoForm extends BaseForm {

private static final long serialVersionUID = 1L;

private String store_no; 编码

private String store_name; 名称

public String getStore_no() {

return store_no;

}

public void setStore_no(String store_no) {

this.store_no = store_no;

}

public String getStore_name() {

return store_name;

}

public void setStore_name(String store_name) {

this.store_name = store_name;

}

}

二、点击导出按钮,跳转到action对应的方法

findShopBillInfoByCondition方法要在service实习 和 在对应的xml里面写,将在下面写上。

@Controller

public class StoreBillInfoAction extends BaseManageAction {

private final String FILE_PATH = this.getClass().getClassLoader().getResource("").getPath();

@RequestMapping(value="/control/store/billinfo/export")

public void export(StoreBillInfoForm formbean, HttpServletResponse response) throws IOException{

Condition condition = new Condition();

QueryResult queryresult = to(StoreBillInfoService.class).findShopBillInfoByCondition(condition);

String fileName = "供应商编码名称表(" + DateTimeUtils.fullStrFormat() + ").xls"; //导出excel文件名称

List list = queryresult.getResultList();

if(list != null && !list.isEmpty()){

BillSheet.billInfoExport(FILE_PATH, list, formbean, fileName, response);

}

}

}

三、同formbean一样 只写需要导出的属性

public class StoreBillInfo extends AbstractEntity{

private static final long serialVersionUID = 1L;

private String store_no;

private String store_name;

public String getStore_no() {

return store_no;

}

public void setStore_no(String store_no) {

this.store_no = store_no;

}

public String getStore_name() {

return store_name;

}

public void setStore_name(String store_name) {

this.store_name = store_name;

}

}

上面model对应的配置文件 xml

resultType="com.ikcsoft.model.store.StoreBillInfo">

SELECT store_no,store_name FROM gl_store

${queryCondition}

SELECT COUNT(*) FROM gl_store

${queryCondition}

四、第二步里面最后的方法 BillSheet.billInfoExport

public static void billInfoExport(String path, List list, StoreBillInfoForm formbean, String fileName, HttpServletResponse response){

try {

String templateName = "xls/storebilllist.xls"; // 这个excel文件是导出excel的模板,需要放在指定位置,比如说第二行第一列写 编码 第二列写名称

InputStream is = new FileInputStream(path + templateName);

HSSFWorkbook workbook = new HSSFWorkbook(is);// 创建 一个excel文档对象

HSSFSheet sheet = workbook.getSheetAt(0);

sheet.setDefaultColumnWidth(15);

sheet.getRow(0).createCell(1).setCellValue(DateTimeUtils.fullFormat()); // 第一行第一列 设置一个时间

if(list != null && !list.isEmpty()){

for (int i = 0; i < list.size(); i ++) {

StoreBillInfo storeBillInfo = list.get(i);

HSSFRow sheetRow = sheet.createRow(i + 2);// 创建一个行对象 从第3行开始 跳过第1行第2行

sheetRow.createCell(0).setCellValue(storeBillInfo.getStore_no());

sheetRow.createCell(1).setCellValue(storeBillInfo.getStore_name());

}

}

response.reset();

response.setHeader("Content-Disposition", "attachment;fileName= " + new String(fileName.getBytes("GBK"),"ISO8859-1"));

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

OutputStream outExcel = response.getOutputStream();

workbook.write(outExcel);

outExcel.close();

workbook.close();

} catch (Exception e) {

logger.error("导出供应商编码名称:", e);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Java 导出 Excel 进行换行的示例代码: ```java // 创建一个新的工作簿 Workbook workbook = new XSSFWorkbook(); // 创建一个新的工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建一行 Row row = sheet.createRow(0); // 创建一个单元格,并设置文本内容 Cell cell = row.createCell(0); cell.setCellValue("这是一个需要换行的文本,\n这是第二行。"); // 设置单元格样式,使文本自动换行 CellStyle style = workbook.createCellStyle(); style.setWrapText(true); cell.setCellStyle(style); // 将工作簿写入到文件中 FileOutputStream outputStream = new FileOutputStream("example.xlsx"); workbook.write(outputStream); outputStream.close(); ``` 在上面的示例代码中,我们创建了一个 `XSSFWorkbook` 对象来表示一个新的工作簿,然后创建了一个名为 "Sheet1" 的新工作表,并在第一行第一列中添加了一个单元格。 我们在单元格中设置了需要换行的文本,并使用 `\n` 字符来表示换行。接着,我们创建了一个 `CellStyle` 对象,并将其设置为自动换行。最后,我们将工作簿写入到一个名为 "example.xlsx" 的文件中。 运行代码后,打开生成的 Excel 文件,可以看到文本已经自动换行了。 需要注意的是,自动换行只在单元格宽度不足以显示所有文本时才会起作用。因此,在实际使用中,需要根据文本长度调整单元格宽度,以确保文本能够正确地显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值