
首先先根据sheet找到行
row = sheetgetRow(rowIndex);
然后找
cell = rowgetCell(1);
这样就去到当前的B1了,
cellgetStringCellValue()
就能取到当前的单元格的value
XSSFWorkbook wb=new XSSFWorkbook(参数);中的参数是InputStream ,你直接XSSFWorkbook wb=new XSSFWorkbook(fs);就可以了。
第一步查询数据--这一步读者自行实现自己的数据查询 List<PointInfo> points = null;
points = thisdaogetAllCollect(userId);
final Map<String, List<PointInfo>> pointMap = new HashMap<>();
for (final PointInfo pointInfo : points) {
final String pt = pointInfogetPointType(); if (pointMapcontainsKey(pt)) {final List<PointInfo> subList = pointMapget(pt);
subListadd(pointInfo);
} else {final List<PointInfo> subList = new ArrayList<>();subListadd(pointInfo);
pointMapput(pt, subList
第二步:生成工作簿
final SXSSFWorkbook wb = new SXSSFWorkbook();
// 对每一种类型生成一个sheet
for (final MapEntry<String, List<PointInfo>> entry : pointMapentrySet()) {
final List<PointInfo> pts = entrygetValue();
// 获取每种类型的名字--作为sheet显示名称--如果不需要分sheet可忽略
String typeName = "";
if (thisdaogetTypeByTypeCode(ptsget(0)getPointType()) != null) {
typeName = thisdaogetTypeByTypeCode(ptsget(0)getPointType())getPointTypeName();
}
final Sheet sheet = wbcreateSheet(typeName);
//生成用于插入的容器--这个方法返回的类型在老api中不同
final Drawing patriarch = sheetcreateDrawingPatriarch();
// 为sheet1生成第一行,用于放表头信息
final Row row = sheetcreateRow(0);
// 第一行的第一个单元格的值
Cell cell = rowcreateCell((short) 0);
cellsetCellValue("详细地址");
cell = rowcreateCell((short) 1);
cellsetCellValue("经度");
cell = rowcreateCell((short) 2);
cellsetCellValue("纬度");
cell = rowcreateCell((short) 3);
for (int i = 0; i < ptssize(); i++) {
final Row each = sheetcreateRow(i + 1);
Cell infoCell = eachcreateCell((short) 0);
infoCellsetCellValue(ptsget(i)getAddrDetail());
infoCell = eachcreateCell((short) 1);
infoCellsetCellValue(ptsget(i)getX());
infoCell = eachcreateCell((short) 2);
infoCellsetCellValue(ptsget(i)getY());
infoCell = eachcreateCell((short) 3);
//查询获取路径信息--该步读者自定义
PointPic pic = thisdaogetPicInfoByPointId(ptsget(i)getId());
try {
if (pic != null) {
for (int k = 0; k < 6; k++) {//因为有六张,所以循环6次
final short colNum = (short) (4+k);
infoCell = eachcreateCell(colNum);
BufferedImage img = null;
switch (k) {
case 0:
if (!StringUtilsisEmpty(picgetPicOneAddr())) {
File imgFile = new File(picgetPicOneAddr());
img = ImageIOread(imgFile);
imgFile = null;
}
break;
case 1:
if (!StringUtilsisEmpty(picgetPicTwoAddr())) {
File imgFile = new File(picgetPicTwoAddr());
img = ImageIOread(imgFile);
imgFile = null;
}
break;
case 2:
if (!StringUtilsisEmpty(picgetPicThreeAddr())) {
File imgFile = new File(picgetPicThreeAddr());
img = ImageIOread(imgFile);
imgFile = null;
}
break;
case 3:
if (!StringUtilsisEmpty(picgetPicFourAddr())) {
File imgFile = new File(picgetPicFourAddr());
img = ImageIOread(imgFile);
imgFile = null;
}
break;
case 4:
if (!StringUtilsisEmpty(picgetPicFiveAddr())) {
File imgFile = new File(picgetPicFiveAddr());
img = ImageIOread(imgFile);
imgFile = null;
}
break;
case 5:
if (!StringUtilsisEmpty(picgetPicSixAddr())) {
File imgFile = new File(picgetPicSixAddr());
img = ImageIOread(imgFile);
imgFile = null;
}
break;
}
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIOwrite(img, "jpg", byteArrayOut);
img = null;
//设置每张插入位置
final XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, colNum,
i + 1, (short) (colNum + 1), i + 2);//参数为插入在表格的坐标,可以自行查看api研究参数
anchorsetAnchorType(0);
// 插入
patriarchcreatePicture(anchor, wbaddPicture(
byteArrayOuttoByteArray(), HSSFWorkbookPICTURE_TYPE_JPEG));
byteArrayOutclose();
byteArrayOut = null;
}
pic = null;
}
} catch (final Exception e) {
eprintStackTrace();
}
}
}
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wbwrite(os);
} catch (final IOException e) {
eprintStackTrace();
}
final byte[] content = ostoByteArray();
final String url = VarBASE_URL+ Fileseparator + "outputxls";//读者自定义路径
final File file = new File(url);// Excel文件生成后存储的位置。
OutputStream fos = null;
try {
fos = new FileOutputStream(file);
foswrite(content);
osclose();
fosclose();
} catch (final Exception e) {
eprintStackTrace();
}
return url;//文件保存成功
方法/步骤
一、锁定表格的行的功能
1、选中第1行,然后单击“视图”选项卡,在“窗口”组中点击“冻结窗格”按钮,接着选择“冻结拆分窗格”。
2、出现如图所示两条黑色实线,然后“视图”选项卡,在“窗口”组中点击“冻结窗格”按钮,接着选择“冻结首行”。然后鼠标滚动滚轮上下就实现首行不动,其他行跟随移动。
二、锁定表格的列的功能
1、选中第1行,然后单击“视图”选项卡,在“窗口”组中点击“冻结窗格”按钮,接着选择“冻结拆分窗格”。
2、出现如图所示两条黑色实线,然后“视图”选项卡,在“窗口”组中点击“冻结窗格”按钮,接着选择“冻结首列”。然后鼠标单击左右三角形滑块就实现首列(本表案例中序号列)不动,其他列跟随移动。
Notes是一个具有Id , RuleID , MainId 属性的javaBean
public static List<Notes> readFromXLS2007(String filePath) {
File excelFile = null;// Excel文件对象
InputStream is = null;// 输入流对象
String cellStr = null;// 单元格,最终按字符串处理
List<Notes> NotesList = new ArrayList<Notes>();// 返回封装数据的List
Notes Notes = null;// 每一个Notes对象
try {
excelFile = new File(filePath);
is = new FileInputStream(excelFile);// 获取文件输入流
XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2007文件对象
XSSFSheet sheet = workbook2007getSheetAt(0);// 取出第一个工作表,索引是0
// 开始循环遍历行,表头不处理,从1开始
for (int i = 1; i <= sheetgetLastRowNum(); i++) {
Notes = new Notes();// 实例化Notes对象
XSSFRow row = sheetgetRow(i);// 获取行对象
if (row == null) {// 如果为空,不处理
continue;
}
// 循环遍历单元格
for (int j = 0; j < rowgetLastCellNum(); j++) {
XSSFCell cell = rowgetCell(j);// 获取单元格对象
if (cell == null) {// 单元格为空设置cellStr为空串
cellStr = "";
} else if (cellgetCellType() == XSSFCellCELL_TYPE_BOOLEAN) {// 对布尔值的处理
cellStr = StringvalueOf(cellgetBooleanCellValue());
} else if (cellgetCellType() == XSSFCellCELL_TYPE_NUMERIC) {// 对数字值的处理
cellStr = cellgetNumericCellValue() + "";
} else {// 其余按照字符串处理
cellStr = cellgetStringCellValue();
}
// 下面按照数据出现位置封装到bean中
if (j == 0) {
NotessetId(new Double(cellStr)intValue());
} else if (j == 1) {
NotessetRuleId(new Double(cellStr)intValue());
} else if (j == 2) {
NotessetMainId(new Double(cellStr)intValue());
}
}
NotesListadd(Notes);// 数据装入List
}
} catch (IOException e) {
eprintStackTrace();
} finally {// 关闭文件流
if (is != null) {
try {
isclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
return NotesList;
}
}
因为在项目中两个表格共用了同一个组件, 但由于表头不完全相同, 就通过v-if来控制表头的显示隐藏,没想到造成了表头顺序的错乱
表头,是表格的开头部分,用于对一些问题的性质的归类。每张调查表按惯例总要有被调查者的简况反映,如被调查者的性别、年龄、学历、收入、家庭成员组成、政治背景、经济状况等。这类问题一般排列在调查表开头部分,称“表头”。
表头设计应根据调查内容的不同有所分别,表头所列项目是分析结果时不可缺少的基本项目。
以上就是关于java用POI *** 作Excel时当遍历单元格时如何获取当前单元个的具体坐标全部的内容,包括:java用POI *** 作Excel时当遍历单元格时如何获取当前单元个的具体坐标、JAVA编程中用Apache POI 怎么用SXSSFWorkbook对已存在的excel(.xlsx) *** 作进行写数据 *** 作、使用poi导出的excel怎么设置表头不动等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)