springmvc excel表格数据导入数据库怎么做

springmvc excel表格数据导入数据库怎么做,第1张

一) 其实这个功能在spring2.x时代就提供了。一直没用过,今天在spring-mvc3.2.x的环境宏孙下试验了一次。还算简单易用。

二) 依赖。

spring依赖POI或jExcel来实现对excel输出的支持,前者是apache出品,貌似名气更大,本例使用第一个。

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.7</version>

</dependency>

三) spring提供了一个AbstractExcelView作为自己实现的视图的父类。实例代码如下。

package ying.car.view

import java.text.DateFormat

import java.text.SimpleDateFormat

import java.util.List

import java.util.Map

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

import org.apache.poi.hssf.usermodel.HSSFDataFormat

import org.apache.poi.hssf.usermodel.HSSFSheet

import org.apache.poi.hssf.usermodel.HSSFWorkbook

import org.apache.poi.ss.usermodel.Cell

import org.apache.poi.ss.usermodel.CellStyle

import org.apache.poi.ss.usermodel.IndexedColors

import org.joda.time.DateTime

import org.slf4j.Logger

import org.slf4j.LoggerFactory

import org.springframework.web.servlet.view.document.AbstractExcelView

import ying.car.binding.DateRange

import ying.car.domain.RefuelingRecord

public class RefuelingRecordExcelView extends AbstractExcelView {

private static final Logger LOGGER = LoggerFactory.getLogger(RefuelingRecordExcelView.class)

private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd")

@Override

@SuppressWarnings({"unchecked"})

protected void buildExcelDocument(

Map<String, Object> model,        // MVC中的M就在这里了

HSSFWorkbook workbook,

HttpServletRequest request,

HttpServletResponse response) 宽纳throws Exception

{

("yyyy/MM/dd"))

LOGGER.debug("end: {}", new DateTime(dr.getEnd()).toString("yyyy/MM/dd"))

}

}

HSSFSheet sheet = workbook.createSheet(DATE_FORMAT.format(dr.getStart()) + "-" + 蔽巧链DATE_FORMAT.format(dr.getEnd()))

setColumnsWidth(sheet)

fillTableHeader(workbook, sheet)

fillTableBody(workbook, sheet, rrl)

}

private void setColumnsWidth(HSSFSheet sheet) {

final int[] warr = new int[] {

500,  // <空>

4500, // 日期

4500, // 车辆

4500, // 燃油种类

4500, // 燃油单价

4500, // 加油方式

4500, // 加油量

3000, // 花费

12000  // 备注

}

for (int i = 0 i < warr.length i ++) {

sheet.setColumnWidth(i, warr[i])

}

}

// 填充表格头

private void fillTableHeader(HSSFWorkbook workbook, HSSFSheet sheet) {

final String[] contents = new String[] {

"日期",

"车辆",

"燃油种类",

"燃油单价(元/升)",

"加油方式",

"加油量(升)",

"花费(元)",

"备注"

}

int r = 1

int c = 1

CellStyle style = workbook.createCellStyle()

style.setFillForegroundColor(IndexedColors.YELLOW.getIndex())      // 填充黄色

style.setFillPattern(CellStyle.SOLID_FOREGROUND)           // 填充方式

// 设置border

style.setBorderLeft(CellStyle.BORDER_THIN)

style.setBorderRight(CellStyle.BORDER_THIN)

style.setBorderTop(CellStyle.BORDER_THIN)

style.setBorderBottom(CellStyle.BORDER_THIN)

for (int i = 0 i < contents.length i ++) {

Cell cell = getCell(sheet, r, c + i)

cell.setCellValue(contents[i])

cell.setCellStyle(style)

}

}

private void fillTableBody(HSSFWorkbook workbook, HSSFSheet sheet, List<RefuelingRecord> records) {

// 通用style

CellStyle style = workbook.createCellStyle()

style.setFillForegroundColor(IndexedColors.WHITE.getIndex())       // 填充白色

style.setFillPattern(CellStyle.SOLID_FOREGROUND)                   // 填充方式

style.setBorderLeft(CellStyle.BORDER_THIN)

style.setBorderRight(CellStyle.BORDER_THIN)

style.setBorderTop(CellStyle.BORDER_THIN)

style.setBorderBottom(CellStyle.BORDER_THIN)

// 日期style

CellStyle dateStyle = workbook.createCellStyle()

dateStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex())       // 填充白色

dateStyle.setFillPattern(CellStyle.SOLID_FOREGROUND)                   // 填充方式

dateStyle.setBorderLeft(CellStyle.BORDER_THIN)

dateStyle.setBorderRight(CellStyle.BORDER_THIN)

dateStyle.setBorderTop(CellStyle.BORDER_THIN)

dateStyle.setBorderBottom(CellStyle.BORDER_THIN)

dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"))

int r = 2

int c = 1

Cell cell = null

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

RefuelingRecord rr = records.get(i)

// 日期

cell = getCell(sheet, r, c + 0)

if (rr.getDate() != null)

cell.setCellValue(rr.getDate())

cell.setCellStyle(dateStyle)

// 车辆

cell = getCell(sheet, r, c + 1)

if (rr.getVehicle().getNickname() != null)

cell.setCellValue(rr.getVehicle().getNickname())

cell.setCellStyle(style)

// 燃油种类

cell = getCell(sheet, r, c + 2)

if (rr.getGasType() != null) {

String s = null

switch (rr.getGasType()) {

case _0: s  = "0号柴油" break

case _93: s = "93号汽油" break

case _97: s = "97号汽油" break

case _98: s = "98号汽油" break

}

cell.setCellValue(s)

}

cell.setCellStyle(style)

// 单价

cell = getCell(sheet, r, c + 3)

if (rr.getPriceOfGas() != null)

cell.setCellValue(rr.getPriceOfGas())

cell.setCellStyle(style)

// 加油方式

cell = getCell(sheet, r, c + 4)

if (rr.getRefuelingType() != null) {

String s = null

switch (rr.getRefuelingType()) {

case FIXED_CUBAGE:

s = "固定容积" break

case FIXED_MONEY:

s = "固定金额" break

case FULL:

s = "加满" break

}

cell.setCellValue(s)

}

cell.setCellStyle(style)

// 加油量

cell = getCell(sheet, r, c + 5)

if (rr.getCubageOfGas() != null)

cell.setCellValue(rr.getCubageOfGas())

cell.setCellStyle(style)

// 花费

cell = getCell(sheet, r, c + 6)

if (rr.getSumOfMoney() != null)

cell.setCellValue(rr.getSumOfMoney())

cell.setCellStyle(style)

// 备注

cell = getCell(sheet, r, c + 7)

if (rr.getComment() != null)

cell.setCellValue(rr.getComment())

cell.setCellStyle(style)

r ++

}

}

}

cell.setCellStyle(style)

// 燃油种类

cell = getCell(sheet, r, c + 2)

if (rr.getGasType() != null) {

String s = null

switch (rr.getGasType()) {

case _0: s  = "0号柴油"break

case _93: s = "93号汽油"break

case _97: s = "97号汽油"break

case _98: s = "98号汽油"break

}

cell.setCellValue(s)

}

cell.setCellStyle(style)

// 单价

cell = getCell(sheet, r, c + 3)

if (rr.getPriceOfGas() != null)

cell.setCellValue(rr.getPriceOfGas())

cell.setCellStyle(style)

// 加油方式

cell = getCell(sheet, r, c + 4)

if (rr.getRefuelingType() != null) {

String s = null

switch (rr.getRefuelingType()) {

case FIXED_CUBAGE:

s = "固定容积"break

case FIXED_MONEY:

s = "固定金额"break

case FULL:

s = "加满"break

}

cell.setCellValue(s)

}

cell.setCellStyle(style)

// 加油量

cell = getCell(sheet, r, c + 5)

if (rr.getCubageOfGas() != null)

cell.setCellValue(rr.getCubageOfGas())

cell.setCellStyle(style)

// 花费

cell = getCell(sheet, r, c + 6)

if (rr.getSumOfMoney() != null)

cell.setCellValue(rr.getSumOfMoney())

cell.setCellStyle(style)

// 备注

cell = getCell(sheet, r, c + 7)

if (rr.getComment() != null)

cell.setCellValue(rr.getComment())

cell.setCellStyle(style)

r ++

}

}

}

四) Controller中返回逻辑视图名 (代码片段)

Java代码

@RequiresUser       // 安全框架用元注释

@RequiresRoles({"ROLE_USER"})

@RequestMapping(value = "/list/excel", method = RequestMethod.GET)

public String listByExcel(

@DateRangeFormat(pattern = "yyyy-MM-dd") @RequestParam("dateRange") DateRange dateRange,

ModelMap modelMap

)

{

}

// 放入model

modelMap.put("dateRange", dateRange)

modelMap.put("refuelingRecordList", gasService.findRefuelingRecordByDateRange(currentUserId, dateRange))

return "refueling-record-list" // 最终返回逻辑视图名

}

五) 为spring-mvc配置多个视图解析器。

<bean class="org.springframework.web.servlet.view.XmlViewResolver">

<property name="order" value="1" /> <!-- order很重要 -->

<property name="location" value="classpath:/META-INF/views.xml" />

</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="order" value="9999" />

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>

六) 效果图

Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。Easyexcel重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然烂梁团需要100M左右内存降低到KB级别,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式。在上层做了模型转换的封装,让使用者更加简单方便。Easyexcel的Github地址:https://github.com/alibaba/easyexcel。

EasyExcel优势

· 注解式自定义 *** 作。

· 输入输出简单,提供输入输出过程的接口

· 支持一定程度的单元格合并等灵活化 *** 作

EasyExcel劣势

· 框架不成熟,1.1.0版本后提供灵活接口的只剩beta版本

· 依然存在一些bug

· 没有一套完整的api

第一步:pom.xml添加依赖,引入jar包:

版本依赖:

第二步:POJO对象编写:

Student.java实体饥橘对象继承BaseRowModel,编写如下:

第三步:编写监听器,实现对导入的excel文档的处理,实例如下:

注:具体数据库入库业务代码本文略过,请对应实现业渣丛务。

第四步:控制类中调用,进入导入页JSP页addBatchArchivesUser.jsp方法为toAddBatchArchivesUser(String backFlag),保存数据方法为:addBatchArchivesUser(@RequestParam("file") MultipartFile mfile,String orgId,String carCategory)代码如下:

注核心要点:一、根据上传文件后缀为xls、xlsx对ExcelReader中传对应的枚举参数,实现对office 2003 以前的版本和之后版本的区分处理;二、通过监听器的构造函数,从控制类传要调用的服务类(userService);三、通过监听器相关的get方法,将处理结果返回,如本文中的listener.getResult()以上三点,对于初次使用者容易出错。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12228164.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存