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,甚至会被变为科学计数法.另外日期格式化也是一个头疼的问题.其 ...
随机推荐
- 算法训练 P1102
算法训练 P1102 时间限制:1.0s 内存限制:256.0MB 定义一个学生结构体类型student,包括4个字段,姓名.性别.年龄和成绩.然后在主函数中定义一个结构体数组( ...
- 20155316 2016-2017-2 《Java程序设计》第5周学习总结
教材学习内容总结 这周总结 try catch语法 异常继承结构 throw finally AutoCloseable接口 Collection Map Lambda表达式 上周总结 三个关键 类与 ...
- Cannot find config.m4. Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module的 解决方法
cp /php-7.1.22/ext/openssl/config0.m4 /usr/local/php/bin/config.m4
- hdu2072 单词数 字典树
字典树裸题 #include<stdio.h> #include<string.h> ][]; ]; int cnt; int ans; void Insert(char *w ...
- LG1397 [NOI2013]矩阵游戏
题意 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的元素,则F[i][ ...
- IEPNGFix 解决IE6支持PNG透明问题
IE6本身是支持索引色透明度(PNG8)格式,但不支持真彩色透明度(PNG24)格式. 使用IE PNG Fix 2.0可以完美解决IE6支持PNG透明问题,而且支持背景定位和重复属性. IE PNG ...
- WINDOWS FTP命令详解
FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用FTP,都会遇到大量的FTP内部命令.熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍之 ...
- RequireJS 学习
几个学习点: 配置模块路径 定义模块 配置不支持 AMD jsonp 服务 text 插件 css 插件
- 【转】每天一个linux命令(52):ifconfig命令
原文网址:http://www.cnblogs.com/peida/archive/2013/02/27/2934525.html 许多windows非常熟悉ipconfig命令行工具,它被用来获取网 ...
- 【转】每天一个linux命令(5):rm 命令
原文网址:http://www.cnblogs.com/peida/archive/2012/10/26/2740521.html 昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中 ...