标签:sys 需要 substring user 来源 org poi 就是 stat
今天做的最多的事情就是纠错了,
通过添加输出语句判断错误来源;

找到错误来源:
wb = new XSSFWorkbook(input);//语句创建错误
网上查询发现是jar包的问题;
下图为poi的jar包各个用途:(本人需要的是excel)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtil {
private static String substring;
public static void main(String[] args) throws IOException{
try {
//输入文件名
System.out.println("开始读入表格");
ExcelUtil.read("D://dns.xls");
} catch (IOException e) {
e.printStackTrace();
System.out.println("读入失败");
}
}
public static void read(String filePath) throws IOException {
//判断是xls还是xlsx
String fileType = filePath.substring(filePath.lastIndexOf(".") + 1,filePath.length());
InputStream input = new FileInputStream(filePath);
Workbook wb = null;
if (fileType.equals("xls")) {
System.out.println("您的excel格式为.xls");
wb = new HSSFWorkbook(input);
System.out.println("成功读取表格");
} else if (fileType.equals("xlsx")) {
wb= new XSSFWorkbook(input);
} else {
System.out.println("您输入的excel格式不正确");
}
//得到一个工作表对象;
System.out.println("得到一个工作表对象");
Sheet sheet = wb.getSheetAt(0);
int rsRows = sheet.getLastRowNum();// 获取sheet表中的总行数
// 遍历行
System.out.println("遍历行");
for (int i=0;i<=rsRows;i++) {
Row row = sheet.getRow(i);
StringBuffer sb = new StringBuffer();
System.out.println("遍历单元格");
//遍历单元格
for(int j=0;j<row.getLastCellNum();j++){
String value = null;
Cell cell = row.getCell(j);
//判断单元格是否为空
System.out.println("判断单元格是否为空");
if(cell==null||cell.equals(null)||cell.getCellType()==HSSFCell.CELL_TYPE_BLANK){//空值
value="null";
}else {
//判断数据类型
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:value = "" + cell.getCellFormula();//公式型
break;
case HSSFCell.CELL_TYPE_NUMERIC:value = "" + cell.getNumericCellValue();//数值型
break;
case HSSFCell.CELL_TYPE_STRING:value = cell.getStringCellValue();//字符串型
break;
}
}
sb.append(value + " ");
substring = sb.substring(0, sb.length()-1);
}
//转换为数组
String[] strings = sb.toString().split(",");
System.out.println(substring.toString());
System.out.println("操作完成!");
for(int a=0;a<strings.length;a++) {
System.out.println(strings[a]);
}
}
}
}
标签:sys 需要 substring user 来源 org poi 就是 stat
原文地址:https://www.cnblogs.com/sengzhao666/p/11142733.html