poi 读取excel数据
excel2003
FileInputStream fin;
try{
fin = newFileInputStream("D:\\workbookxls");
HSSFWorkbook workBook = newHSSFWorkbook(fin);
HSSFSheet sheet = workBookgetSheetAt(0);
for(inti = 0; i < sheetgetLastRowNum(); i++)
{
HSSFRow row = sheetgetRow(i);
HSSFCell cell = rowgetCell((short) 0);
String value= rowgetCell((short) 0)getStringCellValue();
}
}
catch(FileNotFoundExceptione)
{
eprintStackTrace();
} catch(IOException e)
{
eprintStackTrace();
}
2excel2007
XSSFWorkbook workbook;
FileInputStream fin;
try{
fin = newFileInputStream("D:\\workbookxlsx");
workbook = newXSSFWorkbook(fin);
XSSFSheet sheet = workbookgetSheetAt(0);
for(inti = 1; i < sheetgetLastRowNum(); i++)
{
XSSFRow row = sheetgetRow(i);
XSSFCell cell = rowgetCell(1);
String value= celltoString();
}
}
catch(FileNotFoundException e)
{
eprintStackTrace();
} catch(IOException e)
{
eprintStackTrace();
}
我复制了你的代码去运行,运行了一下是有数据的,我就加了两个try catch
XSSFWorkbook xssfWorkbook=null;
try {
xssfWorkbook = new XSSFWorkbook("C:\\Users\\Administratoruser-THINK\\Desktop\\医学影像报告统计xlsx");
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
XSSFWorkbook workbook = new XSSFWorkbook();
for(int numSheet = 0; numSheet < xssfWorkbookgetNumberOfSheets(); numSheet++){
XSSFSheet xssfSheet = xssfWorkbookgetSheetAt(numSheet);
XSSFSheet newSheet = workbookcreateSheet(xssfSheetgetSheetName());
if(xssfSheet == null){
continue;
}
// 循环行Row
for(int rowNum = 0; rowNum <= xssfSheetgetPhysicalNumberOfRows(); rowNum++ ){
XSSFRow xssfRow = xssfSheetgetRow( rowNum);
XSSFRow newRow = newSheetcreateRow( rowNum);
if(xssfRow == null){
continue;
}
// 循环列Cell
for(int cellNum = 0; cellNum <= xssfRowgetPhysicalNumberOfCells(); cellNum++){
XSSFCell xssfCell = xssfRowgetCell(cellNum);
XSSFCell newCell = newRowcreateCell(cellNum);
if(xssfCell == null){
continue;
}
if(xssfCellgetCellType()==0){
newCellsetCellValue(xssfCellgetNumericCellValue());
}else{
newCellsetCellValue(xssfCellgetStringCellValue());
}
}
}
}
Systemoutprintln("-------------");
Systemoutprintln(workbookgetSheetAt(0)getPhysicalNumberOfRows());//这里打出有数据
String outPutx="C:\\Users\\Administratoruser-THINK\\Desktop\\医学影像报告统计2xlsx";
FileOutputStream fOuts=null;
try {
fOuts = new FileOutputStream(outPutx);
workbookwrite(fOuts);//能得到工作表名字,但里面没数据
fOutsflush();
fOutsclose();
} catch (Exception e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
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;//文件保存成功
以上就是关于android读取excel文件第三方类库都有哪些全部的内容,包括:android读取excel文件第三方类库都有哪些、java将数据导出excel计算其文件大小、急急急!!!! 怎样用Java读取EXCEl等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)