下面的代码是向Excel中插入多张的例子:
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
BufferedImage bufferImg1 = null;
try {
// 先把读进来的放到一个ByteArrayOutputStream中,以便产生ByteArray
// 读入1
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIOread(new File("d://test11jpg"));
ImageIOwrite(bufferImg, "jpg", byteArrayOut);
// 读入2
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg1 = ImageIOread(new File("d://test22png"));
ImageIOwrite(bufferImg1, "png", byteArrayOut1);
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wbcreateSheet("test picture");
HSSFPatriarch patriarch = sheet1createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,
(short) 1, 1, (short) 5, 5);
anchorsetAnchorType(3);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 255, 255,
(short) 6, 6, (short) 10, 10);
anchor1setAnchorType(3);
// 插入1
patriarchcreatePicture(anchor, wbaddPicture(byteArrayOut
toByteArray(), HSSFWorkbookPICTURE_TYPE_JPEG));
// 插入2
patriarchcreatePicture(anchor1, wbaddPicture(byteArrayOut1
toByteArray(), HSSFWorkbookPICTURE_TYPE_PNG));
fileOut = new FileOutputStream("d:/workbookxls");
// 写入excel文件
wbwrite(fileOut);
fileOutclose();
} catch (IOException io) {
ioprintStackTrace();
Systemoutprintln("erorr : " + iogetMessage());
} finally {
if (fileOut != null) {
try {
fileOutclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
这样执行后的效果如下:(完全按照HSSFClientAnchor设置的参数显示)
这边的效果没有保持原来的倍率,有点失真了,是因为HSSFClientAnchor(0, 0, 255, 255, (short) 1, 1, (short) 5, 5); 这个构造函数的原因。
HSSFClientAnchor构造函数参数的意义如下:
@param dx1 the x coordinate within the first cell
@param dy1 the y coordinate within the first cell
@param dx2 the x coordinate within the second cell
@param dy2 the y coordinate within the second cell
@param col1 the column (0 based) of the first cell
@param row1 the row (0 based) of the first cell
@param col2 the column (0 based) of the second cell
@param row2 the row (0 based) of the second cell
其中dx1,dy1这个点是定义在开始cell中的起始位置。这个点是开始cell的左上为原点,相对比率来确定的。不是绝对坐标。
dx2,dy2这个点是定义在终了cell的终了位置。这个点是终了cell的左上为原点,相对比率来确定的。不是绝对坐标。
col1,row1是定义开始cell。
col2,row2是定义终了cell。
如果我们想要保持原始的大小,改一下上面代码中的下面部分就可以了。
修改前:patriarchcreatePicture(anchor, wbaddPicture(byteArrayOuttoByteArray(), HSSFWorkbookPICTURE_TYPE_JPEG));
修改后:patriarchcreatePicture(anchor, wbaddPicture(byteArrayOut
toByteArray(), HSSFWorkbookPICTURE_TYPE_JPEG))resize(1);
修改后效果如下:
其中resize是放大或缩小的函数。用了这个函数后,HSSFClientAnchor构造函数中的显示的终了cell位置就不起作用了。
PS:抠门货,一点财富值都不舍得掏啊。。。。鄙视你。
package edusjtuerplabpoi;
import javaioInputStream&ch=wwxqychain" target="_blank" class="link-baike">FileInputStream;
import javaioFileNotFoundException;
import javaioIOException;
import javaioInputStream;
import javatextSimpleDateFormat;
import javautilDate;
import javautilHashMap;
import javautilMap;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFDateUtil;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
import orgapachepoipoifsfilesystemPOIFSFileSystem;
/
*** 作Excel表格的功能类
/
public class ExcelReader {
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
/
读取Excel表格表头的内容
@param InputStream
@return String 表头内容的数组
/
public String[] readExcelTitle(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
eprintStackTrace();
}
sheet = wbgetSheetAt(0);
row = sheetgetRow(0);
// 标题总列数
int colNum = rowgetPhysicalNumberOfCells();
Systemoutprintln("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
//title[i] = getStringCellValue(rowgetCell((short) i));
title[i] = getCellFormatValue(rowgetCell((short) i));
}
return title;
}
/
读取Excel数据内容
@param InputStream
@return Map 包含单元格数据内容的Map对象
/
public Map<Integer, String> readExcelContent(InputStream is) {
Map<Integer, String> content = new HashMap<Integer, String>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
eprintStackTrace();
}
sheet = wbgetSheetAt(0);
// 得到总行数
int rowNum = sheetgetLastRowNum();
row = sheetgetRow(0);
int colNum = rowgetPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheetgetRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(rowgetCell((short) j))trim() +
// "-";
str += getCellFormatValue(rowgetCell((short) j))trim() + " ";
j++;
}
contentput(i, str);
str = "";
}
return content;
}
/
获取单元格数据内容为字符串类型的数据
@param cell Excel单元格
@return String 单元格数据内容
/
private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cellgetCellType()) {
case HSSFCellCELL_TYPE_STRING:
strCell = cellgetStringCellValue();
break;
case HSSFCellCELL_TYPE_NUMERIC:
strCell = StringvalueOf(cellgetNumericCellValue());
break;
case HSSFCellCELL_TYPE_BOOLEAN:
strCell = StringvalueOf(cellgetBooleanCellValue());
break;
case HSSFCellCELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCellequals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
/
获取单元格数据内容为日期类型的数据
@param cell
Excel单元格
@return String 单元格数据内容
/
private String getDateCellValue(HSSFCell cell) {
String result = "";
try {
int cellType = cellgetCellType();
if (cellType == HSSFCellCELL_TYPE_NUMERIC) {
Date date = cellgetDateCellValue();
result = (dategetYear() + 1900) + "-" + (dategetMonth() + 1)
+ "-" + dategetDate();
} else if (cellType == HSSFCellCELL_TYPE_STRING) {
String date = getStringCellValue(cell);
result = datereplaceAll("[年月]", "-")replace("日", "")trim();
} else if (cellType == HSSFCellCELL_TYPE_BLANK) {
result = "";
}
} catch (Exception e) {
Systemoutprintln("日期格式不正确!");
eprintStackTrace();
}
return result;
}
/
根据HSSFCell类型设置数据
@param cell
@return
/
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
// 判断当前Cell的Type
switch (cellgetCellType()) {
// 如果当前Cell的Type为NUMERIC
case HSSFCellCELL_TYPE_NUMERIC:
case HSSFCellCELL_TYPE_FORMULA: {
// 判断当前的cell是否为Date
if (HSSFDateUtilisCellDateFormatted(cell)) {
// 如果是Date类型则,转化为Data格式
//方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
//cellvalue = cellgetDateCellValue()toLocaleString();
//方法2:这样子的data格式是不带带时分秒的:2011-10-12
Date date = cellgetDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellvalue = sdfformat(date);
}
// 如果是纯数字
else {
// 取得当前Cell的数值
cellvalue = StringvalueOf(cellgetNumericCellValue());
}
break;
}
// 如果当前Cell的Type为STRIN
case HSSFCellCELL_TYPE_STRING:
// 取得当前的Cell字符串
cellvalue = cellgetRichStringCellValue()getString();
break;
// 默认的Cell值
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}
public static void main(String[] args) {
try {
// 对读取Excel表格标题测试
InputStream is = new FileInputStream("d:\\test2xls");
ExcelReader excelReader = new ExcelReader();
String[] title = excelReaderreadExcelTitle(is);
Systemoutprintln("获得Excel表格的标题:");
for (String s : title) {
Systemoutprint(s + " ");
}
// 对读取Excel表格内容测试
InputStream is2 = new FileInputStream("d:\\test2xls");
Map<Integer, String> map = excelReaderreadExcelContent(is2);
Systemoutprintln("获得Excel表格的内容:");
for (int i = 1; i <= mapsize(); i++) {
Systemoutprintln(mapget(i));
}
} catch (FileNotFoundException e) {
Systemoutprintln("未找到指定路径的文件!");
eprintStackTrace();
}
}
}
首先先根据sheet找到行
row = sheetgetRow(rowIndex);
然后找
cell = rowgetCell(1);
这样就去到当前的B1了,
cellgetStringCellValue()
就能取到当前的单元格的value
XlsMain java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import javaioFileInputStream;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilList;
import orgapachepoihssfusermodelHSSFCell;
import orgapachepoihssfusermodelHSSFRow;
import orgapachepoihssfusermodelHSSFSheet;
import orgapachepoihssfusermodelHSSFWorkbook;
/
@author Hongten</br>
参考地址:>
以上就是关于poi,如何获取单元格,或者批注框里的背景图片全部的内容,包括:poi,如何获取单元格,或者批注框里的背景图片、java poi怎么获取excel单元格的内容、java用POI *** 作Excel时当遍历单元格时如何获取当前单元个的具体坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)