之前还写过struts2的Excel 的导入导出,也记录下来吧,当时写的时候很蛋疼。。。。。。。
package base.web.actions.file;
import base.service.bo.file.IBoExcelFile;
import base.service.domain.exception.NoSuchBeanException;
import base.service.domain.tools.BeansHelp;
import base.web.actions.BaseAction;
import base.web.entity.FileEntity;
import base.web.tools.WebUtils;
import com.sun.xml.internal.ws.api.policy.PolicyResolver;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.util.CollectionUtils;
import pilot.web.tools.SessionUtil;
import javax.management.remote.JMXPrincipal;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 〈一句话功能简述〉<br>
* 〈excel导入导出〉
*
* @author wy
* @create 2019/7/15
* @since 1.0.0
*/
public class FileAction extends BaseAction {
private File file;
private String fileFileName;
private String fileContentType;
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
private IBoExcelFile boExcelFile;
public FileAction(){
try{
boExcelFile = (IBoExcelFile)BeansHelp.getBeanInstance("boExcelFile");
}catch (NoSuchBeanException e)
{
log.error(e.getMessage());
}
}
/**
* 导出Excel
* @return
*/
public void exportExcel(){
Map map = WebUtils.getParameterMap();
HttpServletResponse response = ServletActionContext.getResponse();
try {
List<FileEntity> list = boExcelFile.queryList(map);
if(!CollectionUtils.isEmpty(list)){
System.out.println("开始导出...");
long s1 = System.currentTimeMillis();
// 开始导出
// excelExport(list, path,response);
ServletOutputStream outFileStearm = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(outFileStearm);
WritableSheet sheet = book.createSheet("第一页", 0);
sheet.setColumnView(0, 5); //第1列宽
sheet.setColumnView(1, 30); //第2列宽
sheet.setColumnView(2, 30); //第3列宽
sheet.setColumnView(3, 30); //第4列宽
sheet.setColumnView(6, 30); //第7列宽
// 设置表头,第一行内容
// Label参数说明:第一个是列,第二个是行,第三个是要写入的数据值,索引值都是从0开始
Label label1 = new Label(0, 0, " 指标ID");// 对应为第1列第1行的数据
Label label2 = new Label(1, 0, "一级分类");// 对应为第2列第1行的数据
Label label3 = new Label(2, 0, "二级分类");// 对应为第3列第1行的数据
Label label4 = new Label(3, 0, "指标名称");// 对应为第4列第1行的数据
Label label5 = new Label(4, 0, "数值");// 对应为第5列第1行的数据
Label label6 = new Label(5, 0, "单位");// 对应为第6列第1行的数据
Label label7 = new Label(6, 0, "统计周期");// 对应为第7列第1行的数据
Label label8 = new Label(7, 0, "数值标准");// 对应为第8列第1行的数据
// 添加单元格到选项卡中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
sheet.addCell(label6);
sheet.addCell(label7);
sheet.addCell(label8);
// 遍历集合并添加数据到行,每行对应一个对象
for (int i = 0; i < list.size(); i++) {
FileEntity fileEntity = list.get(i);
// 表头占据第一行,所以下面行数是索引值+1
// 跟上面添加表头一样添加单元格数据,这里为了方便直接使用链式编程
sheet.addCell(new Label(0, i + 1, fileEntity.getINDEID()));
sheet.addCell(new Label(1, i + 1, fileEntity.getCLAFICATION()));
sheet.addCell(new Label(2, i + 1, fileEntity.getCATION()));
sheet.addCell(new Label(3, i + 1, fileEntity.getINDENAME()));
sheet.addCell(new Label(4, i + 1, fileEntity.getNUMERICAL()));
sheet.addCell(new Label(5, i + 1, fileEntity.getCOMPANY()));
sheet.addCell(new Label(6, i + 1, fileEntity.getSTATIME()));
sheet.addCell(new Label(7, i + 1, fileEntity.getNUMERICALNORM()));
}
response.setContentType("application/msexcel");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("指标导出.xls", "UTF-8"));
response.setCharacterEncoding("utf-8");
book.write();
book.close();
outFileStearm.flush();
outFileStearm.close();
// excelExport(list,response);
this.msg = "导出成功";
long s2 = System.currentTimeMillis();
long time = s2 - s1;
System.out.println("导出完成!消耗时间:" + time + "毫秒");
}
} catch (Exception e)
{
this.code = -1;
this.msg = "导出失败";
this.expMsg = getExceptionMessage(e);
log.error(e.getMessage());
}
}
/**
* Excel导入
* @return
*/
public String importExcel(){
try{
Map map = WebUtils.getParameterMap();
HttpServletRequest request = ServletActionContext.getRequest();
String hospid = SessionUtil.getInstance().getCookie(request,"organizeId");
List<FileEntity> list = excelImport(file);
for (FileEntity fileEntity : list) {
boExcelFile.insertExcelFile(fileEntity,hospid);
System.out.println(fileEntity);
}
System.out.println("导入数据一共【"+list.size()+"】行");
} catch (Exception e)
{
this.code = -1;
this.msg = "导入失败";
this.expMsg = getExceptionMessage(e);
log.error(e.getMessage());
}
return getResult();
}
//导出
public static void excelExport(List<FileEntity> list,HttpServletResponse res) throws IOException {
WritableWorkbook book = null;
try {
OutputStream os = res.getOutputStream();// 取得输出流
res.reset();// 清空输出流
res.setHeader("Content-disposition", "attachment; filename="
+ new String("daily".getBytes("GB2312"),
"iso8859_1") + ".xls");// 设定输出文件头
res.setContentType("application/msexcel");// 定义输出类型
// 创建一个Excel文件对象
//
ServletOutputStream servletOutputStream = res.getOutputStream();
book = Workbook.createWorkbook(servletOutputStream);
// 创建Excel第一个选项卡对象
WritableSheet sheet = book.createSheet("第一页", 0);
sheet.setColumnView(0, 5); //第1列宽
sheet.setColumnView(1, 30); //第2列宽
sheet.setColumnView(2, 30); //第3列宽
sheet.setColumnView(3, 30); //第4列宽
sheet.setColumnView(6, 30); //第7列宽
// 设置表头,第一行内容
// Label参数说明:第一个是列,第二个是行,第三个是要写入的数据值,索引值都是从0开始
Label label1 = new Label(0, 0, " 指标ID");// 对应为第1列第1行的数据
Label label2 = new Label(1, 0, "一级分类");// 对应为第2列第1行的数据
Label label3 = new Label(2, 0, "二级分类");// 对应为第3列第1行的数据
Label label4 = new Label(3, 0, "指标名称");// 对应为第4列第1行的数据
Label label5 = new Label(4, 0, "数值");// 对应为第5列第1行的数据
Label label6 = new Label(5, 0, "单位");// 对应为第6列第1行的数据
Label label7 = new Label(6, 0, "统计周期");// 对应为第7列第1行的数据
Label label8 = new Label(7, 0, "数值标准");// 对应为第8列第1行的数据
// 添加单元格到选项卡中
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
sheet.addCell(label5);
sheet.addCell(label6);
sheet.addCell(label7);
sheet.addCell(label8);
// 遍历集合并添加数据到行,每行对应一个对象
for (int i = 0; i < list.size(); i++) {
FileEntity fileEntity = list.get(i);
// 表头占据第一行,所以下面行数是索引值+1
// 跟上面添加表头一样添加单元格数据,这里为了方便直接使用链式编程
sheet.addCell(new Label(0, i + 1, fileEntity.getINDEID()));
sheet.addCell(new Label(1, i + 1, fileEntity.getCLAFICATION()));
sheet.addCell(new Label(2, i + 1, fileEntity.getCATION()));
sheet.addCell(new Label(3, i + 1, fileEntity.getINDENAME()));
sheet.addCell(new Label(4, i + 1, fileEntity.getNUMERICAL()));
sheet.addCell(new Label(5, i + 1, fileEntity.getCOMPANY()));
sheet.addCell(new Label(6, i + 1, fileEntity.getSTATIME()));
sheet.addCell(new Label(7, i + 1, fileEntity.getNUMERICALNORM()));
}
// 写入数据到目标文件
book.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//导入
public static List<FileEntity> excelImport(File file) {
List<FileEntity> list = new ArrayList<>();
Workbook book = null;
try {
// 获取Excel对象
book = Workbook.getWorkbook(file);
// 获取Excel第一个选项卡对象
Sheet sheet = book.getSheet(0);
// 遍历选项卡,第一行是表头,所以索引数-1
for (int i = 0; i < sheet.getRows() - 1; i++) {
FileEntity fileEntity = new FileEntity();
// 获取第一列第二行单元格对象
Cell cell = sheet.getCell(0, i + 1);
fileEntity.setINDEID(cell.getContents());
// 获取第二行其他数据
// fileEntity.setINDEID(sheet.getCell(1, i + 1).getContents());
fileEntity.setCLAFICATION(sheet.getCell(1, i + 1).getContents());
fileEntity.setCATION(sheet.getCell(2, i + 1).getContents());
fileEntity.setINDENAME(sheet.getCell(3, i + 1).getContents());
fileEntity.setNUMERICAL(sheet.getCell(4, i + 1).getContents());
fileEntity.setCOMPANY(sheet.getCell(5, i + 1).getContents());
fileEntity.setSTATIME(sheet.getCell(6, i + 1).getContents());
fileEntity.setNUMERICALNORM(sheet.getCell(7, i + 1).getContents());
list.add(fileEntity);
}
// 返回导入的数据集合
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public String queryExcelList()
{
try {
Map map = WebUtils.getParameterMapWithPage();
List<FileEntity> list = boExcelFile.queryList(map);
model.put("data",list);
} catch (Exception e) {
this.code = -1;
this.msg = e.getMessage();
this.expMsg = getExceptionMessage(e);
log.error(e.getMessage());
}
return getResult();
}
}

本文详细介绍了使用Struts2框架进行Excel文件的导入和导出操作的方法。包括了具体的代码实现,如创建Excel文件,设置表头,填充数据,以及如何处理文件上传和读取数据。

1988

被折叠的 条评论
为什么被折叠?



