Poi对excel的基本操作
1.创建简单excel
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
Sheet s1=wb.createSheet("第一个sheet页");//创建Sheet页
Row row=s1.createRow(0);//创建数据行
Cell cell=row.createCell(0);//创建单元格
cell.setCellValue(1);
row.createCell(1).setCellValue(1.2);//单元格可写入不同格式的数据
row.createCell(2).setCellValue("啦啦");
row.createCell(3).setCellValue(false);
FileOutputStream file=new FileOutputStream("e://poi_createCell.xls");
wb.write(file);
file.close();
wb.close();
}
2.创建指定日期格式
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
Sheet sheet=wb.createSheet("diyigeSheet页");
Row row=sheet.createRow(0);
Cell cell=row.createCell(0);
cell.setCellValue(new Date());//默认日期类型 42796.89489
CreationHelper creationHelper=wb.getCreationHelper();
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yy-MM-dd:hh ss"));
cell=row.createCell(1);//创建指定日期类型数据 17-03-02:21 39
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
cell=row.createCell(2);//创建指定日期类型数据(第二种方式) 17-03-02:21 39
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
FileOutputStream fs=new FileOutputStream("e://poi_createData.xls");
wb.write(fs);
fs.close();
wb.close();
}
3.读取excel内容,以文本格式展示
public static void main(String[] args) throws Exception {
InputStream is=new FileInputStream("e:\\poi_read.xls");
POIFSFileSystem fs=new POIFSFileSystem(is);
HSSFWorkbook wb=new HSSFWorkbook(fs);
@SuppressWarnings("resource")
ExcelExtractor excelExtractor=new ExcelExtractor(wb);
excelExtractor.setIncludeSheetNames(false);//控制是否读取sheet页名字
System.out.println(excelExtractor.getText());
}
4.设置excel中单元格内容样式
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
Workbook wb=new HSSFWorkbook();
Sheet s1=wb.createSheet("第一个sheet页");
Row row=s1.createRow(0);
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("e:\\demo.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
//设置单元格样式
private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
Cell cell=row.createCell(column);//创建单元格
cell.setCellValue(new HSSFRichTextString("Align It"));//设置值
CellStyle cellStyle=wb.createCellStyle();//创建单元格样式
cellStyle.setAlignment(halign);//设置单元格水平方向对齐方式
cellStyle.setVerticalAlignment(valign);//设置单元格垂直方向对齐方式
cell.setCellStyle(cellStyle);//设置单元格样式
}
5.设置excel中单元格样式
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
Sheet sheet=wb.createSheet("sheet1");
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.GREEN.getIndex());//左边边框颜色
cellStyle.setBorderRight(CellStyle.BORDER_THIN);//右边边框
cellStyle.setRightBorderColor(IndexedColors.RED.getIndex());//右边边框颜色
cellStyle.setBorderTop(CellStyle.BORDER_THIN);//上边边框
cellStyle.setTopBorderColor(IndexedColors.BLUE.getIndex());//上边边框颜色
cell.setCellStyle(cellStyle);
FileOutputStream fileOut=new FileOutputStream("e:\\createExcelDemo08.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
6.设置excel中单元格前景色和背景色
public static void main(String[] args) throws Exception {
HSSFWorkbook wb=new HSSFWorkbook();
Sheet sheet=wb.createSheet("第一个sheet");
Row row=sheet.createRow(1);
Cell cell=row.createCell(1);
cell.setCellValue("sjdsk");
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());//设置背景色
cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
cell.setCellStyle(cellStyle);
Cell cell2=row.createCell(2);
cell2.setCellValue("sjdsk");
CellStyle cellStyle2=wb.createCellStyle();
cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex());//设置前景色
cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell2.setCellStyle(cellStyle2);
FileOutputStream fileOut=new FileOutputStream("e://createDemo10a.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
7.设置excel中合并单元格
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();//定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个sheet页");
Row row=sheet.createRow(1);
Cell cell=row.createCell(1);
cell.setCellValue("单元格合并测试");
sheet.addMergedRegion(new CellRangeAddress(
1,//起始行
2,//结束行
1,//起始列
2//结束列
));
FileOutputStream file=new FileOutputStream("e:\\createExcelDemo11a.xls");
wb.write(file);
file.close();
wb.close();
}
8.设置excel文字样式
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
Sheet sheet=wb.createSheet("第一个sheet页啦");
Row row=sheet.createRow(1);
//创建一个字体处理类
Font font=wb.createFont();
font.setFontHeightInPoints((short)24);//设置字体大小
font.setItalic(true);//设置斜体
font.setStrikeout(true);//设置文字是否划线
CellStyle style=wb.createCellStyle();
style.setFont(font);
Cell cell=row.createCell(1);
cell.setCellValue("there are build a font");
cell.setCellStyle(style);
FileOutputStream file=new FileOutputStream("e:\\createExcelDemo12.xls");
wb.write(file);
file.close();
wb.close();
}
9.设置添加数据到已有的单元格
public static void main(String[] args) throws Exception {
InputStream input=new FileInputStream("e:\\createExcelDemo12.xls");
POIFSFileSystem fs=new POIFSFileSystem(input);
Workbook wb=new HSSFWorkbook(fs);
Sheet sheet=wb.getSheetAt(0);
Row row=sheet.getRow(1);
Cell cell=row.getCell(1);
if(cell==null){
cell=row.createCell(3);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("测试POI添加数据到已有");
FileOutputStream fos=new FileOutputStream("e:\\createExcelDemo12.xls");
wb.write(fos);
fos.close();
wb.close();
}
10.设置excel单元格中可换行
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
Sheet sheet=wb.createSheet("第一个sheet页");
Row row=sheet.createRow(0);
Cell cell=row.createCell(0);
cell.setCellValue("换个行啦\n 换了吗???");
CellStyle cs=wb.createCellStyle();
//设置可以换行
cs.setWrapText(true);
cell.setCellStyle(cs);
//调整下行的高度
row.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints());
//调整单元格宽度
sheet.autoSizeColumn(2);
FileOutputStream fileOut=new FileOutputStream("e:\\createExceDemo14.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
11.设置excel中单元格数据精度
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
FileOutputStream file=new FileOutputStream("e:\\createExcelPOI_DEMO15.xls");
Sheet sheet=wb.createSheet();
CellStyle style;
DataFormat format=wb.createDataFormat();
Row row;
Cell cell;
short rowNum=0;
short cellNum=0;
row=sheet.createRow(rowNum++);
cell=row.createCell(cellNum++);
cell.setCellValue(12.908);
style=wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);//输出12.9
row=sheet.createRow(rowNum++);
cell=row.createCell(cellNum);
cell.setCellValue(111111111111111.90899);
style=wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0,000"));
cell.setCellStyle(style);//输出111,111,111,111,112
wb.write(file);
file.close();
wb.close();
}
Poi对excel的基本操作的更多相关文章
- POI操作Excel
POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...
- poi对excel的基本读写操作
最近简单的弄了下poi对excel的应用,为方便自己以后的使用就把一些基本操作记录下来,其他更复杂的操作可以等以后有需求的时候再来深入了解一番! 写操作: /** * * 层次结构就是workbook ...
- 使用POI导出Excel(二)-利用模板
一.基本操作见: 使用POI导出Excel 二.本次功能需求 给了一个模板,里面有6个sheet页,每页里面都需要填充相应的数据.如图: 三.需求分析 1.分了6个sheet页,每页的数据都不一样,首 ...
- POI对Excel自定义日期格式的读取
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel private List<String[]> rosolveFile(InputStream is, String ...
- poi导出excel
Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...
- JAVA的POI操作Excel
1.1Excel简介 一个excel文件就是一个工作簿workbook,一个工作簿中可以创建多张工作表sheet,而一个工作表中包含多个单元格Cell,这些单元格都是由列(Column)行(Row)组 ...
- POI导出excel日期格式
参考帖子: [1]http://www.ithao123.cn/content-2028409.html [2]http://javacrazyer.iteye.com/blog/894850 再读本 ...
- 使用jxl,poi读取excel文件
作用:在java后台添加一个方法,读取导入的excel内容,根据需要返回相应的sql语句,以完成对临时表的插入操作. 使用jxl读取excel文件 package com.sixthf.bi.sapp ...
- POI读取Excel内容格式化
在用POI读取Excel内容时,经常会遇到数据格式化的问题. 比如:数字12365会变为12365.0;字符串数字123也会变为123.0,甚至会被变为科学计数法.另外日期格式化也是一个头疼的问题.其 ...
随机推荐
- matlab学习笔记 函数bsxfun repmat
一.举例 a=rand(3,1);b=rand(1,3); c=bsxfun(@plus,a,b); d=a*b; c和d的运算类似,只不过c是外加,d是外乘. 作用:速度快>for循环> ...
- Texas Instruments matrix-gui-2.0 hacking -- app_description.php
<?php /* * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ * * * Redistrib ...
- for-auto使用
前言 c++11新增了一个工具,让编译器能够根据初始值的类型推断变量的类型: c++11还新增了一种循环,基于范围的for循环,可以对数组或者容器类的每一个元素执行相同的操作:同时,可以使用& ...
- nginx keepalive 高可用
https://blog.csdn.net/u012410733/article/details/57078407 在网络中机器不可避免的出现单点故障,当我们使用nginx进行反向代理的时候如果出现了 ...
- 王垠:完全用Linux工作 - imsoft.cnblogs
完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...
- 定时器setTimeout()的传参方法
更具体的代码:http://www.cnblogs.com/3body/p/5416830.html // 由于setTimeout()的延迟执行特性,所以在执行的函数中直接使用外部函数的变量是无法获 ...
- ==,equals,hashcode
总结:== 基本类型比较值,引用类型比较是不是同一个对象,也就是比较内存地址 equals 在没有覆盖的情况下是比较 引用的地址的 和 == 一样 hashcode 和equals关系: hashc ...
- Spring插件3.8.2的安装
主机环境:win8 64bit eclipse版本:4.5.2 MARS 插件版本:Spring Tool Suite3.8.2 安装过程:直接在线安装,没有先在官网把插件下载再安装. 主要步骤: 1 ...
- Python自动发邮件——smtplib和email库和yagmail库
''' 一.先导入smtplib模块 导入MIMEText库用来做纯文本的邮件模板 二.发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是 smtp.163.com 三. ...
- JUC线程池之 ThreadPoolExecutor简介
ThreadPoolExecutor简介 ThreadPoolExecutor是线程池类.对于线程池,可以通俗的将它理解为"存放一定数量线程的一个线程集合.线程池允许若个线程同时允许,允许同 ...