最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中。简单回顾一下之前学过的用java操作Excel。

1、maven配置Apache POI

pom.xml中配置POIjar包坐标

 <!-- 配置Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>

2、测试

 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.junit.Test; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; public class POITest { @Test
public void run() throws IOException {
// 1、创建一个工作簿
Workbook wb = new HSSFWorkbook();
// 2、创建一个sheet
Sheet sheet = wb.createSheet();
// 3、创建行对象
Row row = sheet.createRow(2);
// 4、创建单元格
Cell cell = row.createCell(3);
// 5、设置单元格内容
cell.setCellValue("Apache POI操作Excel测试");
// 单元格样式
CellStyle cellStyle = wb.createCellStyle();
// 字体
Font font = wb.createFont();
font.setFontName("华文隶书");
font.setFontHeightInPoints((short)20);
cellStyle.setFont(font);
// 6、设置字体样式
cell.setCellStyle(cellStyle);
// 7、保存,关闭流
OutputStream os = new FileOutputStream("E:\\POITest.xls");
wb.write(os);
os.close();
}
}

3、结果

这个操作是比较简单的,工作需要做的是:首先验证是否是Excel文件,其次验证Excel中的内容,然后读取上传的Excel文件内容(第一行的标题及每行内容),最后将读取的内容插入相关的数据库表。

最新文章

  1. Xcode6新特性(1)-删除Main.storyboard
  2. Thinkphp更改子集
  3. Microsoft Dynamics AX 2012: How to get Company,Customer and Vendor address in AX 2012
  4. php用simplexml来操作xml
  5. linux专题三之如何悄悄破解root密码(以redhat7.2x64为例)
  6. matlab 画图中线型及颜色设置
  7. JAVA正则表达式:Pattern类与Matcher类详解
  8. MyEclipse 优化
  9. 使用Struts+Hibernate开发学生信息管理系统
  10. freebsd上安装nginx+php记录
  11. Delphi 2010初体验,是时候抛弃Delphi 7了
  12. 添加IFrame导致内存溢出的解决过程(IE浏览器,目前发现了原因,还未解决)
  13. &quot;git add -A&quot; is equivalent to &quot;git add .; git add -u&quot;.
  14. POJ-1003&amp;1004
  15. POI操作Excel详细解释,HSSF和XSSF两种方式
  16. Nginx Upstream模块源码分析(上)
  17. testng实现场景恢复
  18. 【最短路+最大流】上学路线@安徽OI2006
  19. listview的gridview视图中,获取列中模板内的button按钮(找控件内的控件)
  20. February 14th, 2018 Week 7th Wednesday

热门文章

  1. JMeter函数整理
  2. K nearest neighbor cs229
  3. CAP原理与最终一致性 强一致性 弱一致性
  4. swoole 定时器 swoole_time_tick 和 swoole_time_after
  5. js特效 15个小demo
  6. Oracle之:Function :numberToDate()
  7. 3 监控项、触发器、web监控、nginx监控
  8. for 循环用了那么多次,但你真的了解它么?
  9. 包管理神器-pipenv
  10. 关于lower_bound()和upper_bound()