java 中使用excl与设置单元格格式
1.pom依赖文件
<poi.version>3.14</poi.version><!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency>
2读取resource目录下的excl文件
String fileName = "templates/test.xlsx";//InputStream resourceAsStream = this.getClass().getResourceAsStream(fileName);InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);if (null == resourceAsStream) {logger.error("文件获取为空,【{}】", fileName);}XSSFWorkbook wb = new XSSFWorkbook(resourceAsStream);XSSFSheet sheet = wb.getSheetAt(0);
//可以debug row中的内容,可以查看到excl中的文件内容XSSFRow row = sheet.getRow(1);
//TODO 行中的内容,或者列中的内容修改
3.获取列中的数据,根据类型获取列的数据,如果直接获取的话会出现数字获取异常等情况
数字类型不能赋值到string类型上面的问题
XSSFRow row = sheet.getRow(行号);if (row == null) {continue;}
short lastCellNum = row.getLastCellNum();if (lastCellNum < 0) {continue;}
for (int i = 0; i < lastCellNum; i++) {XSSFCell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);int cellType = cell.getCellType();String stringCellValue = null;if (Cell.CELL_TYPE_NUMERIC == cellType) {double numericCellValue = cell.getNumericCellValue();stringCellValue = String.valueOf(numericCellValue);} else {stringCellValue = cell.getStringCellValue();}
}
4.eg:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.SubstituteLogger;
import org.springframework.boot.test.context.SpringBootTest;import java.io.FileOutputStream;
import java.io.InputStream;private final static Logger logger = LoggerFactory.getLogger(ExclTest.class);@Testpublic void test1() throws Exception {String fileName = "templates/纸片-批量.xlsx";//InputStream resourceAsStream = this.getClass().getResourceAsStream(fileName);InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName);if (null == resourceAsStream) {logger.error("文件获取为空,【{}】", fileName);}XSSFWorkbook wb = new XSSFWorkbook(resourceAsStream);XSSFSheet sheet = wb.getSheetAt(0);XSSFRow row = sheet.getRow(1);short lastCellNum = row.getLastCellNum();for (int i = 0; i < lastCellNum; i++) {//根据类型获取excl表格中的数据XSSFCell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);int cellType = cell.getCellType();if(cellType == Cell.CELL_TYPE_NUMERIC){double numericCellValue = cell.getNumericCellValue();}}//在excl文件后面添加一列row.createCell(lastCellNum).setCellValue("错误原因");for (int i = 3; i <= 5; i++) {row = sheet.getRow(i - 1);row.createCell(lastCellNum).setCellValue("错误原因" + i);}//输出Excel文件FileOutputStream output = new FileOutputStream("d:\\workbook-zhi.xls");wb.write(output);output.flush();output.close();wb.close();logger.info("write ok ");}
设置单元格格式:
参考文章:https://www.cnblogs.com/yanjie-java/p/8329276.html
package com.myjava.poi;import java.io.FileOutputStream;
import java.util.Date;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;public class ExcelStyle {public static void main(String[] args) throws Exception{Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页Row row=sheet.createRow(2); // 创建一个行row.setHeightInPoints(30);createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);FileOutputStream fileOut=new FileOutputStream("D:\\工作簿.xls");wb.write(fileOut);fileOut.close();}/*** 创建一个单元格并为其设定指定的对齐方式* @param wb 工作簿* @param row 行* @param column 列* @param halign 水平方向对其方式* @param valign 垂直方向对其方式*/private static void createCell(Workbook wb,Row row,short column,short halign,short valign){Cell cell=row.createCell(column); // 创建单元格cell.setCellValue(new HSSFRichTextString("我在这")); // 设置值CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式cellStyle.setAlignment(halign); // 设置单元格水平方向对其方式cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式cell.setCellStyle(cellStyle); // 设置单元格样式 } }
定义边框格式:
package com.myjava.poi;import java.io.FileOutputStream;import java.util.Calendar;import java.util.Date;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.CreationHelper;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;public class Border {public static void main(String[] args) throws Exception{Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页Row row=sheet.createRow(1); // 创建一个行Cell cell=row.createCell(1); // 创建一个单元格cell.setCellValue(4);CellStyle cellStyle=wb.createCellStyle();cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左边边框cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左边边框颜色cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右边边框颜色cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边边框颜色cell.setCellStyle(cellStyle);FileOutputStream fileOut=new FileOutputStream("D:\\Border.xls");wb.write(fileOut);fileOut.close();}
}
设置颜色
package com.myjava.poi;2 3 import java.io.FileOutputStream;4 import java.util.Calendar;5 import java.util.Date;6 7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;8 import org.apache.poi.ss.usermodel.Cell;9 import org.apache.poi.ss.usermodel.CellStyle;
10 import org.apache.poi.ss.usermodel.CreationHelper;
11 import org.apache.poi.ss.usermodel.IndexedColors;
12 import org.apache.poi.ss.usermodel.Row;
13 import org.apache.poi.ss.usermodel.Sheet;
14 import org.apache.poi.ss.usermodel.Workbook;
15
16 public class Bg {
17
18 public static void main(String[] args) throws Exception{
19 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
20 Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
21 Row row=sheet.createRow(1); // 创建一个行
22
23 Cell cell=row.createCell(1);
24 cell.setCellValue("看不清我");
25 CellStyle cellStyle=wb.createCellStyle();
26 cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
27 cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
28 cell.setCellStyle(cellStyle);
29
30
31 Cell cell2=row.createCell(2);
32 cell2.setCellValue("我的前景色与众不同");
33 CellStyle cellStyle2=wb.createCellStyle();
34 cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
35 cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
36 cell2.setCellStyle(cellStyle2);
37
38 FileOutputStream fileOut=new FileOutputStream("D:\\bg.xls");
39 wb.write(fileOut);
40 fileOut.close();
41 }
42 }
单元格合并
package com.myjava.poi;2 3 import java.io.FileOutputStream;4 import java.util.Calendar;5 import java.util.Date;6 7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;8 import org.apache.poi.ss.usermodel.Cell;9 import org.apache.poi.ss.usermodel.CellStyle;
10 import org.apache.poi.ss.usermodel.CreationHelper;
11 import org.apache.poi.ss.usermodel.IndexedColors;
12 import org.apache.poi.ss.usermodel.Row;
13 import org.apache.poi.ss.usermodel.Sheet;
14 import org.apache.poi.ss.usermodel.Workbook;
15 import org.apache.poi.ss.util.CellRangeAddress;
16
17 public class GetTogether {
18
19 public static void main(String[] args) throws Exception{
20 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
21 Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
22 Row row=sheet.createRow(1); // 创建一个行
23
24 Cell cell=row.createCell(1);
25 cell.setCellValue("我们被合并单元格啦!");
26
27 sheet.addMergedRegion(new CellRangeAddress(
28 1, // 起始行
29 2, // 结束行
30 1, // 其实列
31 2 // 结束列
32 ));
33
34
35 FileOutputStream fileOut=new FileOutputStream("D:\\Together.xls");
36 wb.write(fileOut);
37 fileOut.close();
38 }
39 }
java 中使用excl与设置单元格格式相关推荐
- java读取合并单元格_Java POI常用方法,读取单元格的值,设置单元格格式,合并单元格,获取已合并的单元格,导出至本地等...
一.设置单元格格式. 设置单元格边框.单元格背景颜色.单元格对齐方式.单元格字体,设置自动换行. /** Description: 设置单元格格式. * @author : ys. * @date : ...
- java导出excel表格设置行高,POI导出Excel设置单元格格式
使用Apache的POI相关API导出Excel设置单元格格式 栗子,一下各个代码之间的变量是通用的,要是在某个代码块中找不到某个变量,则可以向上找寻 准备工作 InputStream = templ ...
- Aspose.Cells设置单元格格式
使用Aspose.Cells操作Excel时,填写的参数是这样的,显然要不得! 这需要像Excel中的"转换为数字"操作,强大的Aspose.Cells可轻松解决这个问题. //默 ...
- 支持将数据导出到Excel文档的时候设置单元格格式的.NET控件Spire.DataExport
Spire.DataExport for .NET是e-iceblue公司推出的一款数据导出类.NET控件.作为一款专业的数据导出控件,Spire.DataExport for .NET可以帮助开发人 ...
- 设置单元格填充方式_单元格的选择及设置单元格格式
数据输入完毕,接下来可以设置字体.对齐方式.添加边框和底纹等方式设置单元格格式,从而美化工作表.要对单元格进行设置,首先要选中单元格. 选择单元格 选择单元格是指在工作表中确定活动单元格以便在单元格中 ...
- datagridview单元格合并居中_系统地学习Excel第17课,设置单元格格式
上一篇:系统地学习Excel第16课,使用Excel的「替换」功能 本篇内容结构如下: 本章的知识体系 Excel工作表的整体外观由各个单元格的样式构成,单元格的样式外观在Excel的可选设置中主要包 ...
- xlsxwriter设置单元格格式
xlsxwriter设置单元格格式xlsxwriter设置单元格格式xlsxwriter设置单元格格式 常用格式设置 my_format = book.add_format({'fg_color': ...
- NPOI 设置单元格格式
转载:http://blog.csdn.net/xxs77ch/article/details/50237017 NPOI 设置单元格格式 using System; using System.Col ...
- 计算机单元格式,2017《职称计算机》知识点:设置单元格格式
东奥会计在线高级会计师频道提供:设置单元格格式.熟悉教材中每个知识点,掌握基础理论知识,形成知识框架,让你在考场中更加应对自如. 要设置单元格格式,首先要选定单元格或单元格区域,再进行相应设置. 4. ...
最新文章
- Runtime-消息发送和消息转发
- 报错解决方案:ERROR: Cython.Build.cythonize not found.
- 【.Net Framework 体积大?】不安装.net framework 也能运行!?开篇叙述-1
- vue设置cookie的domain无效_【Vue.js入门到实战教程】16Tailwind 与 Bootstrap 的区别和使用入门...
- android get方法的使用方法,android之HttpPostHttpGet使用方法介绍
- windows server 2008 r2 安装
- 如何在苹果设备上查看 Apple Pay 交易?
- python类方法为装饰器_类方法的python装饰器
- TCPIP详解3.8ifconfig
- KDD 2016 | node2vec:Scalable Feature Learning for Networks
- 台式计算机加固态硬盘,台式机加硬盘|台式机加SSD固态硬盘提速教程
- Android8.0通知适配
- 用arduino uno的串口读取JY61角度传感器的角速度、加速度、角度数据MPU6050
- 微信公众号数据2019_年度大榜!2019全国县级媒体公众号百强数据看过来
- matlab根号下是多项式,多项式求根
- Proxmox VE技巧
- windows ubuntu 大华直连抓图
- 两个不同牌子的手机便签怎么换机
- 敏捷个人2012.5月份户外活动报道:0费用京郊经典户外路线【京西古道】
- 微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)