将页面的列表数据导出到excel文件中
2017-12-12 17:21
543 查看
1、用的是springMVC的模式
2、在service接口层
//根据页面传过来的id在库里查询
List<TbItem> selectByPrimaryKeydhh(long ids);
在service接口的实现类中
@Override
public List<TbItem> selectByPrimaryKeydhh(long ids) {
//执行查询的sql
List<TbItem> selectByPrimaryKey = tbItemMapper.selectByPrimaryKeydhh(ids);
return selectByPrimaryKey;
}
3、在controller层
@Controller
public class Example {
//定义文件的格式
private static final ReportFileTypeEnum X = ReportFileTypeEnum.XLS;
@Autowired
private ItemService itemService;
@RequestMapping(value="/partExportdhh",method=RequestMethod.POST)
public void maiddhhn(long ids) {
List<Model> dataCode0 = new ArrayList<Model>();
List<TbItem> list = itemService.selectByPrimaryKeydhh(ids);
for (int i = 0; i < list.size(); i++) {
Model model=new Model();
TbItem tbItem = list.get(i);
model.set("id",tbItem.getId());
model.set("title",tbItem.getTitle());
model.set("sell_point",tbItem.getSellPoint());
model.set("price",tbItem.getPrice());
model.set("num",tbItem.getNum());
model.set("barcode",tbItem.getBarcode());
model.set("image",tbItem.getImage());
model.set("cid",tbItem.getCid());
model.set("status",tbItem.getStatus());
model.set("created",tbItem.getCreated());
model.set("updated",tbItem.getUpdated());
dataCode0.add(model);
}
LinkedHashMap<String, String> headersCode = new LinkedHashMap<String, String>();
headersCode.put("id","商品ID");
headersCode.put("title","商品标题");
headersCode.put("sell_point","叶子类目");
headersCode.put("price","价格");
headersCode.put("num","库存数量");
headersCode.put("barcode","条形码");
headersCode.put("image","图片");
headersCode.put("cid","商品类目ID");
headersCode.put("status","状态");
headersCode.put("created","创建时间");
headersCode.put("updated","更新日期");
//code2列表的模板
String code2temlateUrl = "D:\\export\\code2" + X.getStatusDesc();
//实际导出来的数据
String copytemplate2Url = "D:\\export\\copy" + X.getStatusDesc();
//如果不存在这个文件夹就会创建这个文件夹
if (!new File("D:\\export").exists()) {
new File("D:\\export").mkdir();
}
HashMap<Integer, Integer> width = new HashMap<>();
//设置这个excle会导出到少个列
width.put(0, 3);
width.put(1, 3);
width.put(2, 3);
width.put(3, 3);
width.put(4, 3);
width.put(5, 3);
width.put(7, 3);
width.put(8, 3);
width.put(9, 3);
width.put(10, 3);
// 根据模板导出数据
Utils.code2Tempalte(code2temlateUrl, dataCode0.size(), "商品列表", 12, X, width,
headersCode.values().toArray());
File file = new File(code2temlateUrl);
if (!file.exists() || !file.isFile()) {
System.out.println("文件不存在");
return;
}
Utils.copyTemplate(file, copytemplate2Url, X);
try {
Workbook wb = Utils.getWorkbookInstance(X, new FileInputStream(copytemplate2Url));
int rowNo = 2;
int rowSum = 0;
Utils.file2FileWithData(wb, X, dataCode0, rowNo, 0, 12,headersCode.keySet().toArray());
rowSum = 2 + dataCode0.size();
FileOutputStream out = null;
out = new FileOutputStream(copytemplate2Url);
wb.write(out);
System.out.println("导出成功");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
4、对于controller中用到的几个工具类
//判断excel文件的后缀格式
public enum ReportFileTypeEnum {
XLSX(0, ".xlsx"),
XLS(1, ".xls");
int status;
String statusDesc;
private ReportFileTypeEnum(int status,String statusDesc){
this.status=status;
this.statusDesc=statusDesc;
}
public int getStatus(){
return status;
}
public String getStatusDesc(){
return statusDesc;
}
public static ReportFileTypeEnum getType(int status){
for (ReportFileTypeEnum excelFileTypeEnums : ReportFileTypeEnum.values()) {
if (excelFileTypeEnums.getStatus() == status) {
return excelFileTypeEnums;
}
}
return null;
}
public static String getStatusDesc(int status){
return getType(status).getStatusDesc();
}
}
//用来封装实体类
public class Model{
private Map<String, Object> attrs = new HashMap<String, Object>();
public void set(String attr, Object value) {
attrs.put(attr, value);
}
public Object get(String attr) {
return attrs.get(attr);
}
public Set entrySet() {
return attrs.entrySet();
}
4、需要引入poi的jar包的
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
2、在service接口层
//根据页面传过来的id在库里查询
List<TbItem> selectByPrimaryKeydhh(long ids);
在service接口的实现类中
@Override
public List<TbItem> selectByPrimaryKeydhh(long ids) {
//执行查询的sql
List<TbItem> selectByPrimaryKey = tbItemMapper.selectByPrimaryKeydhh(ids);
return selectByPrimaryKey;
}
3、在controller层
@Controller
public class Example {
//定义文件的格式
private static final ReportFileTypeEnum X = ReportFileTypeEnum.XLS;
@Autowired
private ItemService itemService;
@RequestMapping(value="/partExportdhh",method=RequestMethod.POST)
public void maiddhhn(long ids) {
List<Model> dataCode0 = new ArrayList<Model>();
List<TbItem> list = itemService.selectByPrimaryKeydhh(ids);
for (int i = 0; i < list.size(); i++) {
Model model=new Model();
TbItem tbItem = list.get(i);
model.set("id",tbItem.getId());
model.set("title",tbItem.getTitle());
model.set("sell_point",tbItem.getSellPoint());
model.set("price",tbItem.getPrice());
model.set("num",tbItem.getNum());
model.set("barcode",tbItem.getBarcode());
model.set("image",tbItem.getImage());
model.set("cid",tbItem.getCid());
model.set("status",tbItem.getStatus());
model.set("created",tbItem.getCreated());
model.set("updated",tbItem.getUpdated());
dataCode0.add(model);
}
LinkedHashMap<String, String> headersCode = new LinkedHashMap<String, String>();
headersCode.put("id","商品ID");
headersCode.put("title","商品标题");
headersCode.put("sell_point","叶子类目");
headersCode.put("price","价格");
headersCode.put("num","库存数量");
headersCode.put("barcode","条形码");
headersCode.put("image","图片");
headersCode.put("cid","商品类目ID");
headersCode.put("status","状态");
headersCode.put("created","创建时间");
headersCode.put("updated","更新日期");
//code2列表的模板
String code2temlateUrl = "D:\\export\\code2" + X.getStatusDesc();
//实际导出来的数据
String copytemplate2Url = "D:\\export\\copy" + X.getStatusDesc();
//如果不存在这个文件夹就会创建这个文件夹
if (!new File("D:\\export").exists()) {
new File("D:\\export").mkdir();
}
HashMap<Integer, Integer> width = new HashMap<>();
//设置这个excle会导出到少个列
width.put(0, 3);
width.put(1, 3);
width.put(2, 3);
width.put(3, 3);
width.put(4, 3);
width.put(5, 3);
width.put(7, 3);
width.put(8, 3);
width.put(9, 3);
width.put(10, 3);
// 根据模板导出数据
Utils.code2Tempalte(code2temlateUrl, dataCode0.size(), "商品列表", 12, X, width,
headersCode.values().toArray());
File file = new File(code2temlateUrl);
if (!file.exists() || !file.isFile()) {
System.out.println("文件不存在");
return;
}
Utils.copyTemplate(file, copytemplate2Url, X);
try {
Workbook wb = Utils.getWorkbookInstance(X, new FileInputStream(copytemplate2Url));
int rowNo = 2;
int rowSum = 0;
Utils.file2FileWithData(wb, X, dataCode0, rowNo, 0, 12,headersCode.keySet().toArray());
rowSum = 2 + dataCode0.size();
FileOutputStream out = null;
out = new FileOutputStream(copytemplate2Url);
wb.write(out);
System.out.println("导出成功");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
4、对于controller中用到的几个工具类
//判断excel文件的后缀格式
public enum ReportFileTypeEnum {
XLSX(0, ".xlsx"),
XLS(1, ".xls");
int status;
String statusDesc;
private ReportFileTypeEnum(int status,String statusDesc){
this.status=status;
this.statusDesc=statusDesc;
}
public int getStatus(){
return status;
}
public String getStatusDesc(){
return statusDesc;
}
public static ReportFileTypeEnum getType(int status){
for (ReportFileTypeEnum excelFileTypeEnums : ReportFileTypeEnum.values()) {
if (excelFileTypeEnums.getStatus() == status) {
return excelFileTypeEnums;
}
}
return null;
}
public static String getStatusDesc(int status){
return getType(status).getStatusDesc();
}
}
//用来封装实体类
public class Model{
private Map<String, Object> attrs = new HashMap<String, Object>();
public void set(String attr, Object value) {
attrs.put(attr, value);
}
public Object get(String attr) {
return attrs.get(attr);
}
public Set entrySet() {
return attrs.entrySet();
}
4、需要引入poi的jar包的
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
相关文章推荐
- java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- JFinal框架使用renderFile()进行列表页面数据导出Excel
- aspx页面中,DataTable中的数据导出到Excel文件中
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- Java程序员从笨鸟到菜鸟之(一百零六)java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- 导出List列表中的数据到Excel文件,并提供下载
- Java程序员从笨鸟到菜鸟之(一百零六)java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- java操作office和pdf文件页面列表导出cvs,excel、pdf报表.
- java操作office和pdf文件页面列表导出cvs,excel、pdf报表.
- php页面数据列表实现excel导出功能
- Java程序员从笨鸟到菜鸟之(一百零六)java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- java操作office和pdf文件页面列表导出cvs,excel、pdf报表.
- jsp页面导出当前list数据列表 excel
- JavaScript将页面表格数据导出为Excel、CSV格式文件(结合JQuery EasyUI的grid )
- Java导出页面数据或数据库数据至Excel文件并下载,采用JXL技术,小demo(servlet实现)
- Vue2.0---将页面中表格数据导出excel (详细)
- 解决JAVA导出大数据的EXCEL文件
- 导出table列表数据到excel(第5种方法适用多个浏览器)
- datagrid数据导出到excel文件给客户端下载的几种方法