java excel处理框架,Java三方—-excel框架之POI的使用一

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。pdf框架之IText的使用,参见我的博客:Java三方—->pdf框架之IText的使用。今天我们开始POI中Excel部分的学习。

POI框架的简单实例

我们通过案例来学习POI,项目结构如下:

e7e2ccdcd868e0487eed05a0b5afaf08.png

一、 创建工作薄

@Test

public void createExcel() throws Exception {

// 建立xls文件,只需要引入poi-3.14-20160307.jar

Workbook wb1 = new HSSFWorkbook();

FileOutputStream fileOut = new FileOutputStream("excel/workbook.xls");

wb1.write(fileOut);

fileOut.close();

wb1.close();

// 需要额外引入poi-ooxml-3.14-20160307.jar, xmlbeans-2.6.0.jar, poi-ooxml-schemas-3.14-20160307.jar

Workbook wb2 = new XSSFWorkbook();

FileOutputStream fileOut2 = new FileOutputStream("excel/workbook.xlsx");

wb2.write(fileOut2);

fileOut2.close();

wb2.close();

}

二、 创建工作表

@Test

public void createExce2() throws Exception {

Workbook wb = new HSSFWorkbook();

wb.createSheet("new sheet");

wb.createSheet("second sheet");

String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]s"); // returns " O'Brien's sales "

wb.createSheet(safeName);

FileOutputStream fileOut = new FileOutputStream("excel/workbook1.xls");

wb.write(fileOut);

fileOut.close();

wb.close();

}

a67f32f0a09a8194c5d428d316969e09.png

注意: sheet name is Excel must not exceed 31 characters and must not contain any of the any of the following characters:

0x0000

0x0003

colon (:)

backslash (\)

asterisk (*)

question mark (?)

forward slash (/)

opening square bracket ([)

closing square bracket (])

You can use org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)} for a safe way to create valid names, this utility replaces invalid characters with a space (‘ ‘)

三、 创建单元格

@Test

public void createExce3() throws Exception {

Workbook wb = new HSSFWorkbook();

CreationHelper createHelper = wb.getCreationHelper();

Sheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.

Row row = sheet.createRow((short) 0);

// Create a cell and put a value in it.

Cell cell = row.createCell(0);

cell.setCellValue(1);

// Or do it on one line.

row.createCell(1).setCellValue(1.2);

row.createCell(2).setCellValue(createHelper.createRichTextString("This is a string"));

row.createCell(3).setCellValue(true);

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook2.xls");

wb.write(fileOut);

fileOut.close();

wb.close();

}

46978f16353a3f9c92a2f51076ac353b.png

四、 创建时间单元格

@Test

public void createExce4() throws Exception {

Workbook wb = new HSSFWorkbook();

CreationHelper createHelper = wb.getCreationHelper();

Sheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.

Row row = sheet.createRow(0);

// Create a cell and put a date value in it. The first cell is not

// styled

// as a date.

Cell cell = row.createCell(0);

cell.setCellValue(new Date());

// we style the second cell as a date (and time). It is important to

// create a new cell style from the workbook otherwise you can end up

// modifying the built in style and effecting not only this cell but

// other cells.

CellStyle cellStyle = wb.createCellStyle();

cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));

cell = row.createCell(1);

cell.setCellValue(new Date());

cell.setCellStyle(cellStyle);

// you can also set date as java.util.Calendar

cell = row.createCell(2);

cell.setCellValue(Calendar.getInstance());

cell.setCellStyle(cellStyle);

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook3.xls");

wb.write(fileOut);

fileOut.close();

wb.close();

}

cd64a701a7c4fc5c4bfc6030e48310cd.png

五、 不同类型的单元格

@Test

public void createExce5() throws Exception {

Workbook wb = new HSSFWorkbook();

Sheet sheet = wb.createSheet("new sheet");

Row row = sheet.createRow((short) 2);

row.createCell(0).setCellValue(1.1);

row.createCell(1).setCellValue(new Date());

row.createCell(2).setCellValue(Calendar.getInstance());

row.createCell(3).setCellValue("a string");

row.createCell(4).setCellValue(true);

row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook4.xls");

wb.write(fileOut);

fileOut.close();

wb.close();

}

e3d65bb848f6a7beffd386719efcbd4a.png

六、 边框的使用

@Test

public void createExce6() throws Exception {

Workbook wb = new HSSFWorkbook();

Sheet sheet = wb.createSheet("new sheet");

Row row = sheet.createRow(1);

// Create a cell and put a value in it.

Cell cell = row.createCell(1);

cell.setCellValue(4);

// Style the cell with borders all around.

CellStyle style = wb.createCellStyle();

style.setBorderBottom(CellStyle.BORDER_THIN);

style.setBottomBorderColor(IndexedColors.BLACK.getIndex());

style.setBorderLeft(CellStyle.BORDER_THIN);

style.setLeftBorderColor(IndexedColors.GREEN.getIndex());

style.setBorderRight(CellStyle.BORDER_THIN);

style.setRightBorderColor(IndexedColors.BLUE.getIndex());

style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);

style.setTopBorderColor(IndexedColors.BLACK.getIndex());

cell.setCellStyle(style);

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook5.xls");

wb.write(fileOut);

fileOut.close();

wb.close();

}

4352092156e8e1f45a798f06d6954551.png

七、合并单元格

@Test

public void createExce7() throws Exception {

Workbook wb = new HSSFWorkbook();

Sheet sheet = wb.createSheet("new sheet");

Row row = sheet.createRow((short) 1);

Cell cell = row.createCell((short) 1);

cell.setCellValue("This is a test of merging");

sheet.addMergedRegion(new CellRangeAddress(1, // first row (0-based)

1, // last row (0-based)

1, // first column (0-based)

2 // last column (0-based)

));

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook6.xls");

wb.write(fileOut);

fileOut.close();

wb.close();

}

a34deb1fcb5c18a3d744d5f0bdcc5c0c.png

八、 字体的设置

@Test

public void createExce8() throws Exception {

Workbook wb = new HSSFWorkbook();

Sheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.

Row row = sheet.createRow(1);

// Create a new font and alter it.

Font font = wb.createFont();

font.setFontHeightInPoints((short) 24);

font.setFontName("Courier New");

font.setItalic(true);

font.setStrikeout(true);

// Fonts are set into a style so create a new one to use.

CellStyle style = wb.createCellStyle();

style.setFont(font);

// Create a cell and put a value in it.

Cell cell = row.createCell(1);

cell.setCellValue("This is a test of fonts");

cell.setCellStyle(style);

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook7.xls");

wb.write(fileOut);

fileOut.close();

wb.close();

}

62ad589b38537243f63a5c38c5062844.png

九、 读或重写工作表

@Test

public void createExce9() throws Exception {

InputStream inp = new FileInputStream("excel/workbook4.xls");

Workbook wb = WorkbookFactory.create(inp);

Sheet sheet = wb.getSheetAt(0);

Row row = sheet.getRow(2);

Cell cell = row.getCell(3);

if (cell == null)

cell = row.createCell(3);

cell.setCellType(Cell.CELL_TYPE_STRING);

cell.setCellValue("a test");

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook8.xls");

wb.write(fileOut);

fileOut.close();

}

a0b1981040844ef8883ca738c766b914.png

十、 单元格换行

@Test

public void createExce10() throws Exception {

Workbook wb = new XSSFWorkbook(); // or new HSSFWorkbook();

Sheet sheet = wb.createSheet();

Row row = sheet.createRow(2);

Cell cell = row.createCell(2);

cell.setCellValue("Use \n with word wrap on to create a new line");

// to enable newlines you need set a cell styles with wrap=true

CellStyle cs = wb.createCellStyle();

cs.setWrapText(true);

cell.setCellStyle(cs);

// increase row height to accomodate two lines of text

row.setHeightInPoints((2 * sheet.getDefaultRowHeightInPoints()));

// adjust column width to fit the content

sheet.autoSizeColumn((short) 2);

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("excel/workbook9.xlsx");

wb.write(fileOut);

fileOut.close();

wb.close();

}

e4d8ddb454c64893506d1eaa02de0dbd.png

友情链接

https://www.cnblogs.com/huhx/tag/java/default.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/364606.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于background-*的一些属性

1、盒模型 盒模型从外到内一次为:margin-box、border-box、padding-box、content-box。 2、一些属性设置的相对位置 ⑴background-position的属性值(top/right/bottom/left/center)起始位置是相对于padding-box外边沿开始的,…

设计模式:不可变的嵌入式构建器

上周,我写了关于什么使图案成为反图案。 本周,我提出一种设计模式…或等待……也许这是一种反模式。 还是? 让我们看看! 当有一个类可以构建另一个实例时,构建器模式是一种编程样式。 构建器模式的最初目的是将对象的…

Outlook邮件的右键菜单中添加自定义按钮

customUI代码如下&#xff1a; <customUI xmlns"http://schemas.microsoft.com/office/2009/07/customui"><contextMenus><contextMenu idMso"ContextMenuMailItem"> <button id"button1" label"修改件名"…

vue 项目的I18n国际化之路

I18n (internationalization ) ---未完善 产品国际化是产品后期维护及推广中重要的一环&#xff0c;通过国际化操作使得产品能更好适应不同语言和地区的需求 国际化重点&#xff1a;1、 语言语言本地化2、 文化颜色、习俗等3、 书写习惯日期格式、时区、数字格式、书写方向备…

华中师范大学邮箱matlab,18春[华中师范大学]华师《Matlab基础与应用》在线作业1(100分)...

试卷总分:100 得分:100第1题,用round函数四舍五入对数组[2.48 6.39 3.93 8.52]取整&#xff0c;结果为( )。A、[2 6 3 8]B、[2 6 4 8]C、[2 6 4 9]D、[3 7 4 9 ]第2题,下列变量名中( )是合法的。A、char_1B、x*yC、x\yD、end第3题,指出下列错误的指令是( )。A、syms a b;…

nginx 篇

nginx 安装 下载必要组件 nginx下载地址 http://nginx.org/en/download.htmlpcre库下载地址&#xff0c;nginx需要 http://sourceforge.net/projects/pcre/files/pcre/zlib下载地址&#xff0c;nginx需要 http://www.zlib.net/openssl下载地址&#xff0c;nginx需要 https://gi…

使用IAM保护您的AWS基础架构

在开发新产品并发现合适的产品市场时&#xff0c;每个团队都需要快速行动。 尤其是初创公司&#xff0c;因为公司的整个未来都取决于快速找到为您的产品付款的人。 对于初创企业和其他团队来说&#xff0c; Amazon Web Services是令人难以置信的工具&#xff0c;可以快速构建其…

Linux命令集锦:tmux命令

tmux是一款优秀的终端复用软件&#xff0c;平时用到的强大功能有下面两个&#xff1a; 窗口管理&#xff1a;同时启用多个窗口&#xff1b; 保护现场&#xff1a;连接到远程主机之后&#xff0c;一旦断开&#xff0c;那么当前账户登录的任务就被取消了&#xff0c;但是使用 tmu…

一个页面从输入URL到加载显示完成,发生了什么?

面试经典题——URL加载 一、涉及基本知识点&#xff1a; 1. 计算机网络 五层因特尔协议栈&#xff1a; 应用层&#xff08;dns、http&#xff09;&#xff1a;DNS解析成IP并完成http请求发送&#xff1b;传输层&#xff08;tcp、udp&#xff09;&#xff1a;三次握手四次挥手…

mysql文件软连接失败,解决打包软链接打包失败问题

一般情况下打包文件时&#xff0c;如果直接打包软连接会导致打包失败&#xff0c;即没有将要打包的内容打包进去&#xff0c;这里提供tar打包参数-h[rootlocalhost ~]# ll /etc/rc.locallrwxrwxrwx. 1 root root 13 Nov 24 00:45 /etc/rc.local -> rc.d/rc.local[rootlocalh…

快速掌握前端 专为Java程序员定制

Javascript 例子 修改页面内容 js 代码位置 <script>// js 代码 </script>引入 js 脚本 <script src"js脚本路径"></script>注意&#xff0c;到了框架之后&#xff0c;引入方式会有不同 1. 变量与数据类型 声明变量 1) let ⭐️ l…

实施Jersey 2 Spring集成

Jersey是Oracle提供的出色的Java JAX-RS规范参考实现。 去年&#xff0c;当我们开始为大容量网站构建RESTful后端Web服务时&#xff0c;我们选择使用JAX-RS API作为我们的REST框架和Spring框架来进行依赖项注入。 泽西岛是我们选择的JAX-RS实现。 项目启动时&#xff0c;JAX-R…

Solidity中如何判断mapping中某个键是否为空呢?

Solidity中如何判断mapping中某个键是否为空呢&#xff1f; 一.比较标准的做法是建立一个专门和value相关的结构体&#xff0c;用一个布尔型变量来看是否这个key所对应的value被赋过值 代码如下&#xff1a; pragma solidity ^0.4.19;contract UserTest {struct User{string na…

Angular网络请求的封装

很多时候&#xff0c;我很喜欢angular的编码风格&#xff0c;特别是angular支持typescript之后&#xff0c;完整的生命周期&#xff0c;完美的钩子函数&#xff0c;都是别的语言所无法替代的。这里我来说说我自己的网络请求封装&#xff0c;某种意义上来说&#xff0c;angular自…

mac安装了多版本php 卸载,mac 安装多版本PHP

前言相信大家在mac 安装PHP多版本的时候也遇到了很多坑# brew install php56# brew install php70这样安装的话肯定会报错的&#xff0c;因为brew存在软连接这个时候我们第一步&#xff1a;brew unlink php56 或者 brew unlink php70这个步骤是关闭掉PHP的软连接第二步&#x…

新国标电动自行车目录库

浙江&#xff1a;https://xzsp.zjidb.com/api/bicycle 上海&#xff1a;http://www.shbicycle.com/info.asp 北京&#xff1a;http://wfcxjk1.bjjtgl.gov.cn/fjdcml/fjdcListM.jsp 安徽&#xff1a;http://ddch.aqi.ah.cn/index_GB17761-1999.asp 3C查询&#xff1a;http://ccc…

HTML | CSS | JavaScript 常见错误

持续更新 超链接鼠标悬浮后的状态 a:hover 拼写图片文件的路径问题转载于:https://www.cnblogs.com/lcchy/p/10139389.html

隐藏的东西? 您需要HiddenSidesPane

我的甘特图用户之一希望在屏幕上使用尽可能多的空间&#xff0c;并询问是否可以删除滚动条。 但是&#xff0c;如何在没有滚动条的情况下进行导航&#xff1f; 好的&#xff0c;有各种各样的键盘快捷键&#xff0c;当然还有FlexGanttFX支持的普通鼠标拖动&#xff0c;但是大多数…

jQuery的on绑定click和直接绑定click区别

状况之外 在之前的公司并没有遇到这个问题&#xff0c;也就没有深究。直到自己换了现在的公司&#xff0c;刚来第二天就开始写别人写到一半的项目&#xff0c;很无奈&#xff0c;不是原生就是jquery&#xff0c;由于项目急&#xff0c;已经来不及切换框架重新布局&#xff0c;只…

php教程哪个软件好,写php用哪款软件好?解决方法

写php用哪款软件好&#xff1f;现在用php-eclipse&#xff0c;但是感觉不太好用js、html、css的提示功能没有&#xff0c;要装插件&#xff0c;装了很久没装上。想问一下现在开发php哪款软件好大家指导一下&#xff0c;谢谢------解决方案--------------------如果要js、html、…