//poi导入excel表头后缀名是.xls
StringBuffer buffer = new StringBuffer();
buffer.append("<table cellpadding='0' border='1' cellspacing='0' bordercolor='#dcdcdc' style='font-size:13px;width:100%'>");
try {
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
InputStream is = new FileInputStream(path);
XSSFWorkbook xwb = new XSSFWorkbook(is);
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
// 定义 row、cell
XSSFRow row;
String cell = null;
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
buffer.append("<tr>");
row = sheet.getRow(i);int columns = 1;
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
//获取单元格内容,
cell = row.getCell(j).toString();
if(cell == ""){
columns++;
}
}
for (int j = row.getFirstCellNum(); j <= row.getPhysicalNumberOfCells(); j++) {
//获取单元格内容,
cell = row.getCell(j).toString();
buffer.append("<td align='center' height='32'");
if(row.getPhysicalNumberOfCells()==columns){
buffer.append("colspan='"+columns+"'>"+cell+"</td>");
j = columns;
}else{
buffer.append("colspan='1'>"+cell+"</td>");
}
}
buffer.append("</tr>");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
buffer.append("</table>");

这篇博客介绍了如何使用Apache POI库来解析.xls格式的Excel文件,特别是关注于导入表头数据的部分。
3243

被折叠的 条评论
为什么被折叠?



