Excel表格网

poi怎么设置excel2010(poi操作excel方法)

来源:www.0djx.com  时间:2022-11-21 15:42   点击:114  编辑:表格网  手机版

1. poi操作excel方法

“poi”是(Program of Instruction )教学大纲的意思。

“poi”释义

①POI是“Point of Interest”的缩写,可以翻译成“兴趣点”,也有些叫做“Point of Information”,即“信息点”。电子地图上一般用气泡图标来表示POI,像电子地图上的景点、政府机构、公司、商场、饭馆等,都是POI。

②POI是基于位置服务的最核心数据,在电子地图上运用场景广泛, 如导航前选择的目的地、查看周边的餐馆等。

例句:

①The application and importance of POI in logistics monitoring system are analyzed.

本文分析了POI数据在物流监控系统中的应用特点和重要性。

②Support for persistence of Java objects to Excel via JDO/ JPA APIs, utilizing Apache POI.

支持通过JDO/JPA APIs将Java对象持久化到Excel中,这利用了Apache POI。

③This article demonstrates how to use Java technology and the Apache POI to read from Employee_List.xls.

本文演示如何使用Java技术和Apache POI来读取Employee List.xls。

④This article uses the Apache POI because of its support community and rich functionality.

由于Apache POI的支持社区和丰富功能,本文使用Apache POI。

⑤Apache POI is a set of Java APIs for working with both older and newer Microsoft standard documents.

Apache POI是一组用于处理旧版和新版Microsoft标准文档的Java API。

2. poi操作Excel

导出时自由选择路径的代码如下:

1、后台输出Excel文件代码:

OutputStream output = response.getOutputStream();

response.reset();

response.setHeader("Content-disposition", "attachment; filename=" + path);

response.setContentType("Content-Type:application/vnd.ms-excel ");

wb.write(output);

output.close();

2、前端代码:

window.open("getExcelList","_blank");

3. poi excel函数

通过POI的使用获取到要导出的Excel工作簿,将其写入输出流。

然后输出流内容会随响应信息传导浏览器前端,弹出保存窗口,选择你要保存的位置。

4. poi读取excel的方式

“poi”是(Program of Instruction )教学大纲的意思。

“poi”释义

①POI是“Point of Interest”的缩写,可以翻译成“兴趣点”,也有些叫做“Point of Information”,即“信息点”。电子地图上一般用气泡图标来表示POI,像电子地图上的景点、政府机构、公司、商场、饭馆等,都是POI。

②POI是基于位置服务的最核心数据,在电子地图上运用场景广泛, 如导航前选择的目的地、查看周边的餐馆等。

例句:

①The application and importance of POI in logistics monitoring system are analyzed.

本文分析了POI数据在物流监控系统中的应用特点和重要性。

②Support for persistence of Java objects to Excel via JDO/ JPA APIs, utilizing Apache POI.

支持通过JDO/JPA APIs将Java对象持久化到Excel中,这利用了Apache POI。

③This article demonstrates how to use Java technology and the Apache POI to read from Employee_List.xls.

本文演示如何使用Java技术和Apache POI来读取Employee List.xls。

④This article uses the Apache POI because of its support community and rich functionality.

由于Apache POI的支持社区和丰富功能,本文使用Apache POI。

⑤Apache POI is a set of Java APIs for working with both older and newer Microsoft standard documents.

Apache POI是一组用于处理旧版和新版Microsoft标准文档的Java API。

5. poi打开excel

空的CELL不是不处理,这个数据是插入数据库的,所以有些是可以未空的,而且输入Excel表数据不是我输的,是客户输入的,如果用户不是对Excel很熟,等一下出现空行看上去没数据,其实空行上的CELL都市空值,poi也会读的,所以我想解决这个BUG

6. poi生成Excel

首先要导入spring相关包,poi,和fileupload包,我是使用maven构建的。

一.导入excel

(1)使用spring上传文件

a.前台页面提交

<form name="excelImportForm" action="${pageContext.request.contextPath}/brand/importBrandSort" method="post" onsubmit="return checkImportPath();" enctype="multipart/form-data" id="excelImportForm">

<input type="hidden" name="ids" id="ids">

<div >

<div >

<label ><input id="excel_file" type="file" name="filename" accept="xls"/></label>

<div >

<input id="excel_button" type="submit" value="导入Excel"/>

</div>

</div>

</div>

<div >

<button type="button" data-dismiss="modal" onClick="uncheckBoxes();">取消</button>

</div>

b.后台spring的controller进行相关操作,这里主要讲的是使用spring上传文件,和读取文件信息。

使用spring上传文件之前,需要配置bean。

<bean id="multipartResolver" ></bean>@RequestMapping(value = "/importBrandSort", method = RequestMethod.POST)

public ModelAndView importBrandSort(@RequestParam("filename") MultipartFile file,

HttpServletRequest request,HttpServletResponse response) throws Exception {

String temp = request.getSession().getServletContext()

.getRealPath(File.separator)

+ "temp"; // 临时目录

File tempFile = new File(temp);

if (!tempFile.exists()) {

tempFile.mkdirs();

}

DiskFileUpload fu = new DiskFileUpload();

fu.setSizeMax(10 * 1024 * 1024); // 设置允许用户上传文件大小,单位:位

fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:位

fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录

// 开始读取上传信息

//

int index = 0;

/* List fileItems = null;

try {

fileItems = fu.parseRequest(request);

}

catch (Exception e) {

e.printStackTrace();

}

Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件

FileItem fileItem = null;

while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表单信息

if (!item.isFormField()) {

fileItem = item;

// index++;

}

}

if (fileItem == null)

return null;

*/

if (file == null)

return null;

logger.info(file.getOriginalFilename());

String name = file.getOriginalFilename();// 获取上传文件名,包括路径

//name = name.substring(name.lastIndexOf("\\") + 1);// 从全路径中提取文件名

long size = file.getSize();

if ((name == null || name.equals("")) && size == 0)

return null;

InputStream in = file.getInputStream();

List<BrandMobileInfoEntity> BrandMobileInfos = brandService

.importBrandPeriodSort(in);

// 改为人工刷新缓存KeyContextManager.clearPeriodCacheData(new

// PeriodDimensions());// 清理所有缓存

int count = BrandMobileInfos.size();

String strAlertMsg ="";

if(count!=0){

strAlertMsg= "成功导入" + count + "条!";

}else {

strAlertMsg = "导入失败!";

}

logger.info(strAlertMsg);

//request.setAttribute("brandPeriodSortList", BrandMobileInfos);

//request.setAttribute("strAlertMsg", strAlertMsg);

request.getSession().setAttribute("msg",strAlertMsg);

return get(request, response);

//return null;

}

代码中的注释部分是如果不使用spring的方式,如何拿到提交过来的文件名(需要是要apache的一些工具包),其实使用spring的也是一样,只是已经做好了封装,方便我们写代码。

代码中的后半部分是读取完上传文文件的信息和对数据库进行更新之后,输出到前台页面的信息。

上述代码中: InputStream in = file.getInputStream();

List<BrandMobileInfoEntity> BrandMobileInfos = brandService

.importBrandPeriodSort(in);读取excel的信息。

(2)使用poi读取excel

a.更新数据库

@Override

public List<BrandMobileInfoEntity> importBrandPeriodSort(InputStream in) throws Exception {

List<BrandMobileInfoEntity> brandMobileInfos = readBrandPeriodSorXls(in);

for (BrandMobileInfoEntity brandMobileInfo : brandMobileInfos) {

mapper.updateByConditions(brandMobileInfo);

}

return brandMobileInfos;

}

这部分是sevice层的代码,用于读取excel信息之后更新数据库数据,我这里是使用mybatis。定义一个类BrandMobileInfoEntity,用与保存excel表每一行的信息,而List< BrandMobileInfoEntity > 则保存了全部信息,利用这些信息对数据库进行更新。

b.读取excel信息

private List<BrandMobileInfoEntity> readBrandPeriodSorXls(InputStream is)

throws IOException, ParseException {

HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

List<BrandMobileInfoEntity> brandMobileInfos = new ArrayList<BrandMobileInfoEntity>();

BrandMobileInfoEntity brandMobileInfo;

// 循环工作表Sheet

for (int numSheet = 0;

numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {

HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

if (hssfSheet == null) {

continue;

}

// 循环行Row

for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {

brandMobileInfo = new BrandMobileInfoEntity();

HSSFRow hssfRow = hssfSheet.getRow(rowNum);

for (int i = 0; i < hssfRow.getLastCellNum(); i++) {

HSSFCell brandIdHSSFCell = hssfRow.getCell(i);

if (i == 0) {

brandMobileInfo.setBrandId(Integer

.parseInt(getCellValue(brandIdHSSFCell)));

} else if (i == 1) {

continue;

} else if (i == 2) {

brandMobileInfo.setMobileShowFrom(Integer.parseInt(getCellValue(brandIdHSSFCell)));

} else if (i == 3) {

brandMobileInfo.setMobileShowTo(Integer.parseInt(getCellValue(brandIdHSSFCell)));

} else if (i == 4) {

brandMobileInfo.setSellMarkValue(getCellValue(brandIdHSSFCell));

} else if (i == 5) {

brandMobileInfo.setWarehouse(getCellValue(brandIdHSSFCell));

} else if (i ==

7. poi处理excel

你试试设置一下单元格的编码 cell.setCellType(HSSFCell.ENCODING_UTF_8);

8. poi技术实现Excel导入

利用jquery里的ocupload这个js组件,步骤和实现文件上传一样,将你要导入的excel表格进行上传到服务端,然后在工程里导入apache的poi这poi-ooxml以及poi-schemas这三个jar包,接受到文件对象后,利用里面的HSSWorkBook这个对象来解析excel里的每行数据,通过对应的数据类型对象添加到数据库里即可

顶一下
(0)
0%
踩一下
(0)
0%
相关评论
我要评论
用户名: 验证码:点击我更换图片