返回目录:excel表格制作
太笼统了,只能给出一个POI读取Excel的大致方法。
对数据的校验,与具体的文件有关。
你定义的那一列是时间类型的,那一列是数字列的。
盲目的读取,无法判断数据的类型。
如果是在windows 下运行的,且已经安装了Excel的,可以考虑使用jocob。Poi是流操作,只能查文档。
按列读取e68a84e8a2ade79fa5e98193333的方法:
String pathname = "E:\\files\\title.xlsx";
File file = new File(pathname);
InputStream in = new FileInputStream(file);
//得到整个excel对象
XSSFWorkbook excel = new XSSFWorkbook(in);
//获取整个excel有多少个sheet
int sheets = excel.getNumberOfSheets();
//便利第一个sheet
Map<String,String> colMap = new HashMap<String, String>();
for(int i = 0 ; i < sheets ; i++ ){
XSSFSheet sheet = excel.getSheetAt(i);
if(sheet == null){
continue;
}
int mergedRegions = sheet.getNumMergedRegions();
XSSFRow row2 = sheet.getRow(0);
Map<Integer,String> category = new HashMap<Integer, String>();
for(int j = 0 ; j < mergedRegions; j++ ){
CellRangeAddress rangeAddress = sheet.getMergedRegion(j);
int firstRow = rangeAddress.getFirstColumn();
int lastRow = rangeAddress.getLastColumn();
category.put(rangeAddress.getFirstColumn(), rangeAddress.getLastColumn()+"-"+row2.getCell(firstRow).toString());
}
//便利每一行
for( int rowNum = 1 ; rowNum <= sheet.getLastRowNum() ; rowNum++ ){
System.out.println();
XSSFRow row = sheet.getRow(rowNum);
if(row == null){
continue;
}
short lastCellNum = row.getLastCellNum();
String cate = "";
Integer maxIndex = 0;
for( int col = row.getFirstCellNum() ; col < lastCellNum ; col++ ){
XSSFCell cell = row.getCell(col);
if(cell == null ){
continue;
}
if("".equals(cell.toString())){
continue;
}
int columnIndex = cell.getColumnIndex();
String string = category.get(columnIndex);
if(string != null && !string.equals("")){
String[] split = string.split("-");
cate = split[1];
maxIndex = Integer.parseInt(split[0]);
System.out.println(cate+"<-->"+cell.toString());
}else {
//如果当前便利的列编号小于等于合并单元格的结束,说明分类还是上面的分类名称
if(columnIndex<=maxIndex){
System.out.println(cate+"<-->"+cell.toString());
}else {
System.out.println("分类未知"+"<-->"+cell.toString());
}
}
}
}
}
}
试试免费版free Spire.XLS for Java的批注读取方法:e68a84e8a2ad7a64337
import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ExtractComment {
public static void main(String[] args)throws IOException {
//加载excel文档
Workbook wb = new Workbook();
wb.loadFromFile("AddComments.xlsx");
//获取工作表
Worksheet sheet = wb.getWorksheets().get(0);
//打印指定单元格的文本批注内容
System.out.println("H2 的批注文本 = " + sheet.getCellRange("H2").getComment().getText());
System.out.println("F2 的批注文本 = " + sheet.getCellRange("F2").getComment().getRichText().getRtfText());
//获取指定单元格中批注背景颜色
Color color = sheet.getRange().get("H2").getComment().getFill().getForeColor();
System.out.print(color);
//获取指定单元格中的批注背景图片
/*BufferedImage image = sheet.getRange().get("H2").getComment().getFill().getPicture();
ImageIO.write(image,"png",new File("ExtractedImages.png"));*/
}
}