如何提高公众号关注人数最多的公众号?

POI使用小结 - 技术在于积累 - ITeye技术网站
博客分类:
本文主要是针对本人在实际项目中,利用POI来开发数据导入和导出的应用而总结而来。其实JAVA在处理Window OLE 2复合文档格式操作的API,还有很多POI、JExcelApi、IText等,而在实际的个人感觉POI确实是一个不错的选择。下面就POI开发的相关内容进行简要的介绍下,包括POI组成、开发步骤、具体示例、知识点汇总等内容进行讲解。POI组成
POI组成
? 官方网站: http://jakarta.apache.org/poi/,这个是POI的官方网站,用户可以先从这里下载。最新的版本3.0.2,下载解压后里面有三个jar包(poi-3.0.2-FINAL-.jar/ poi-contrib-3.0.2-FINAL-.jar/ poi-scratchpad-3.0.2-FINAL-.jar),而在这里包中主要含:
org.apache.poi.hssf.eventmodel&&& 处理在读取和写入Excel文档中生成的各种动作.
&&&
org.apache.poi.hssf.eventusermodel&&& 提供读取Excel文档的各个类.
&&&
org.apache.poi.hssf.record.formula&&& 包含公式处理类, 这些类使用在Excel文档中.
&&&
org.apache.poi.hssf.usermodel&&& 包含生成Excel文档的各个类.
&&&
org.apache.poi.hssf.util&&& 提供处理Excel文档各个属性的工具类.
开发步骤
1:先下载POI包;
2:解压后将里面的3个jar文件拷贝至WEB-INF\lib中。
3:刷新工程后,即可以使用jar包中的内容了。
? 读取POI文档
一:以文件名创建一个InputStream
二:以上面的InputStream实例创建一个HSSFWorkbook.
三:HSSFWorkbook的getSheetAt(index)可获取相应的工作页
四:工作页调用方法获取行
五:行再获取相应的格,调用单元格的方法,然后取出对应数据。
? 创建Excel文件
一:以new HSSFWorkbook()构造一个新的Excel文档
二:以HSSFWorkbook的createSheet("工作本名"),创建一个新的工作页。
三:新的工作页以createRow(RowIndex)依次创建行
四:HSSFRow以createCell(CellIndex)依次创建单元格
五:设置相应单元格的格式,值。
具体示例
示例一:读取Excel文件中的所有数据,包括对个工作表(薄)。
public static void ReadExcel2(String file) {
int total = 0;
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
int sn = wb.getNumberOfSheets();
for(int i=0;i&i++){
HSSFSheet sheet = wb.getSheetAt(i);
for(int j=0;j&sheet.getLastRowNum();j++){
HSSFRow row = sheet.getRow(j);
for(int k=0;k&row.getLastCellNum();k++){
HSSFCell cell = row.getCell((short)k);
if(cell.getCellType()==HSSFCell.CELL_TYPE_STRING){
System.out.println(cell.getStringCellValue());
System.out.println(cell.getBooleanCellValue());
System.out.println(total);
} catch (Exception e) {
System.out.println(e.getStackTrace());
示例一:创建一个Excel,并把相应的数据添入到指定的单元格。
public static boolean createExcel(String file){
boolean flag =
// wb,对应一个内存中的excel文档
&&&&&&& HSSFWorkbook wb = new HSSFWorkbook();
&&&&&&& // sheet创建一个工作页
&&&&&& for(int h =0;h&3;h++){
&&&&&&& HSSFSheet sheet = wb.createSheet("jzh"+h);
&&&&&&& //设置列的宽度
&&&&&&& sheet.setDefaultColumnWidth((short)20);
&&&&&&& //String[] s = new String[]{"姓名","年龄","地址","城市","公司","工作","职位","收入"};
&&&&&&& String[] s = {"姓名","年龄","地址","城市","公司","工作","职位","收入"};
&&&&&&& cellStyle = getAnyCellStyle(wb,getHdrFont(wb,12),HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, (short)-1, true);
&&&&&&& for(int x=0;x&s.x++){
HSSFRow row = sheet.createRow(0);
&&&&&&&&&&& //设置行的高度
&&&&&&&&&&& row.setHeight((short)500);
&&&&&&&&&&& HSSFCell cell = row.createCell((short)x);
&&&&&&&&&&&&&&& cell.setEncoding(HSSFCell.ENCODING_UTF_16); &&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& cell.setCellValue(s[x]);
&&&&&&&&&&&&&&& cell.setCellStyle(cellStyle);
&&&&&&& for (short i = 1; i & 100; i++)
&&&&&&&&&&& // HSSFRow,对应一行
&&&&&&&&&&& HSSFRow row = sheet.createRow(i);
&&&&&&&&&&& //设置行的高度
&&&&&&&&&&& row.setHeight((short)500);
&&&&&&&&&&& for (short j = 0; j & 8; j++)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& // HSSFCell对应一格
&&&&&&&&&&&&&&& HSSFCell cell = row.createCell(j);
&&&&&&&&&&&&&&& cell.setEncoding(HSSFCell.ENCODING_COMPRESSED_UNICODE); &&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& //cell.setCellValue("Java,我的信仰" + String.valueOf(i));
&&&&&&&&&&&&&&& cell.setCellValue(true);
&&&&&&&&&&& }
&&&&&&& OutputStream out = new FileOutputStream(file);
&&&&&&& wb.write(out);
&&&&&&& out.close();
catch(Exception ex){
}
知识点汇总
? 设置单元格格式
1. 创建字体,设置其为红色、粗体:
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
2、创建格式
HSSFCellStyle cellStyle= workbook.createCellStyle();
cellStyle.setFont(font);
3、应用格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("标题"); 
? 获得这个工作表数据行数
System.out.println(sheet.getLastRowNum());
? 获得列数
System.out.println(row.getLastCellNum());
? 设置第三列的宽度为5
sheet.setColumnWidth((short)3,(short)5);
? 设置字体格式
HSSFCellStyle titlestyle = workbook.createCellStyle();
HSSFFont titlefont = workbook.createFont();
titlefont.setFontHeightInPoints((short)18);
titlefont.setFontName("黑体");
titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titlestyle.setFont(titlefont);
titlestyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titlestyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFCell titlecell = titlerow.createCell((short)((fileds.length) / 3));
titlecell.setCellStyle(titlestyle);
titlecell.setEncoding(HSSFCell.ENCODING_UTF_16);
titlecell.setCellValue(titles);
? 合并单元格
主要是从(1,3)到(5,6)的合并点
sheet.addMergedRegion(new Region((short)1,(short)3,(short)5,(short)6));
浏览: 207678 次
来自: 南京
不好意思我看错了
好吧,网上很多方法一的,我不知道大家有验证过没有。我测试了下。 ...
TonyLian 写道下这个包到WEB-INF/libs sp ...
.ComFailException: ...
没有附件?getAnyCellStyle 这个方法,getHd ...关于POI模版的导入 - ITeye问答
现在有2个excel文件,一个是模版文件1.xls,里面有各种单元格的格式或者设置的什么的,另一个文件是有数据的等待操作的Excel文件2.xls,请问怎么通过java POI 代码实现将2.xls获得模版文件1.xls的各种单元格格式等,求详细代码谢谢!
采纳的答案
这个对你也许有用,基于poi3.6写的比较通用的excel读取,如果有单元格合并的话就不适用了。
package com.dp.
import java.io.BufferedInputS
import java.io.BufferedW
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileW
import java.io.IOE
import java.lang.reflect.InvocationTargetE
import java.lang.reflect.M
import java.util.ArrayL
import java.util.D
import java.util.HashM
import java.util.I
import java.util.L
import java.util.M
import org.apache.poi.ss.usermodel.C
import org.apache.poi.ss.usermodel.R
import org.apache.poi.ss.usermodel.S
import org.apache.poi.hssf.usermodel.HSSFW
import org.apache.poi.ss.usermodel.W
import org.apache.poi.xssf.usermodel.XSSFW
* Excel通用处理器
* @author jkxydp
public class AllPurposeExcelProcesser {
* 通过调用POI读Excel的较为通用的方式,其中约定:excel的第一行中每列的内容为字符串,并且与你定义的model中的字段相对应,你的类遵循JavaBean标准
* @param &T& 你希望读取后组装的数据结构定义
* @param excel 你要读取的文件
* @param modelType 你希望组装的数据的定义的字节码,该字节码描述中必须包含一个无参构造器
* @param sheetName 你要读取的文件中的工作表的名称
* @return 一个装载了读取后获得对象的List
* @throws FileNotFoundException
* @throws IOException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
public static &T& List&T& read(File excel,Class&T& modelType,String sheetName) throws FileNotFoundException, IOException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
Method[] methods = modelType.getMethods();
Workbook wb = getWorkBook(excel);
if(wb == null)
List&T& models = new ArrayList&T&(); //创建容器,用于装置所有读取出来的model
Sheet sheet = wb.getSheet(sheetName); //根据提供的sheet名称拿到整张表
int rows = sheet.getPhysicalNumberOfRows();System.out.println(rows);
Row zeroRow = sheet.getRow(0); //拿到第零行表格,解析列与字段之间的对应关系
Map&Integer,Method& colMark = new HashMap&Integer, Method&(); //存储方法与字段的对应关系
for(short i = 0; i & zeroRow.getLastCellNum(); i ++){
Cell tip = zeroRow.getCell(i);
if(null != tip && tip.getCellType() == Cell.CELL_TYPE_STRING) {
String curTipName = tip.getStringCellValue();
for(int j = 0; j & methods. j ++) {
if(("set" + curTipName.toUpperCase().charAt(0) + curTipName.substring(1)).equals(methods[j].getName())) {
colMark.put((int)i, methods[j]);
Cell curCell =
boolean flag =
for(int i = 1; i & rows - 1; i ++) {
Row aModel = sheet.getRow(i);
if(null != aModel) {
T model = modelType.getConstructor().newInstance();
for(int col = 0; col & colMark.size(); col ++) {
curCell = aModel.getCell(col);
if(null != curCell){
switch (curCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
case Cell.CELL_TYPE_BLANK:
if(colMark.get(col).getParameterTypes()[0] == String.class) {
colMark.get(col).invoke(model, curCell.getStringCellValue());
case Cell.CELL_TYPE_FORMULA:
if(colMark.get(col).getParameterTypes()[0] == String.class) {
colMark.get(col).invoke(model, Integer.toString((int)Math.round(curCell.getNumericCellValue())));
case Cell.CELL_TYPE_NUMERIC:
if(colMark.get(col).getParameterTypes()[0] == int.class || colMark.get(col).getParameterTypes()[0] == Integer.class) {
colMark.get(col).invoke(model, (int)Math.round(curCell.getNumericCellValue()));
} else if (colMark.get(col).getParameterTypes()[0] == Double.class || colMark.get(col).getParameterTypes()[0] == double.class) {
colMark.get(col).invoke(model, curCell.getNumericCellValue());
} else if(colMark.get(col).getParameterTypes()[0] == Float.class || colMark.get(col).getParameterTypes()[0] == float.class) {
colMark.get(col).invoke(model, (float)curCell.getNumericCellValue());
} else if(colMark.get(col).getParameterTypes()[0] == Long.class || colMark.get(col).getParameterTypes()[0] == long.class) {
colMark.get(col).invoke(model, Math.round(curCell.getNumericCellValue()));
} else if(colMark.get(col).getParameterTypes()[0] == Short.class || colMark.get(col).getParameterTypes()[0] == short.class) {
colMark.get(col).invoke(model, (short)Math.round(curCell.getNumericCellValue()));
} else if(colMark.get(col).getParameterTypes()[0] == Byte.class || colMark.get(col).getParameterTypes()[0] == byte.class) {
colMark.get(col).invoke(model, (byte)Math.round(curCell.getNumericCellValue()));
} else if(colMark.get(col).getParameterTypes()[0] == Date.class) {
colMark.get(col).invoke(model, curCell.getDateCellValue());
} else if(colMark.get(col).getParameterTypes()[0] == String.class){
colMark.get(col).invoke(model, Integer.toString((int)Math.round(curCell.getNumericCellValue())));
case Cell.CELL_TYPE_BOOLEAN:
if(colMark.get(col).getParameterTypes()[0] == Boolean.class || colMark.get(col).getParameterTypes()[0] == boolean.class) {
colMark.get(col).invoke(model, curCell.getBooleanCellValue());
if(flag) models.add(model);
* Excel处理差异
private static Workbook getWorkBook(File excel) throws FileNotFoundException, IOException {
return excel.getName().endsWith("xls") ?
new HSSFWorkbook(new BufferedInputStream(new FileInputStream(excel))) :
excel.getName().endsWith("xlsx") ?
new XSSFWorkbook(new BufferedInputStream(new FileInputStream(excel))):
如果不牵扯到list的循环输出和单元格的合并,不是很难写。
1、将模版文件1.xls读入到Workbook对象A中
2、将数据文件2.xls读入到Workbook对象B中
3、循环读取Workbook对象B中所有的数据设置到Workbook对象A里
4、导出Workbook对象A到新的xls
你获取需要填充的单元格的格式就好了,其余的东西直接copy模板的那个文件就OK了
将2.xls 的文件取出来。导入1.xls的格式中?
这代码 估计得写一大阵子。。
先提供下方案。首先先解析 数据的excel 然后循环向1.xls中写入数据。并生产新的xls做为1
按模板导入不是这么搞的,你定义个模板,如你的exacel1,让用户下载该模板,在对应的单元格中填写内容,然后导入,获取excel1的格式意义只在于导出系统数据保持格式风格一样。
已解决问题
未解决问题 上传我的文档
 下载
 收藏
5年编程开发经验,熟悉C++,单片机等领域的开发。
 下载此文档
正在努力加载中...
excel导入导出通用方法
下载积分:600
内容提示:excel导入导出通用方法
文档格式:DOCX|
浏览次数:20|
上传日期: 23:23:31|
文档星级:
该用户还上传了这些文档
excel导入导出通用方法
官方公共微信

我要回帖

更多关于 微信公众号关注人数 的文章

 

随机推荐