下面的程序运行2次就报 文件错误,数据可能丢失
public class tttt {
//创建excel批注测试
public void testExcel()
{
HSSFWorkbook dataBook=null;
try{
FileInputStream fis = new FileInputStream("E:/excelImpTest/testData.xls");
POIFSFileSystem poiFs = new POIFSFileSystem(fis);
dataBook=new HSSFWorkbook(poiFs);
}catch(Exception e)
{
e.printStackTrace();
}
HSSFSheet sheet=dataBook.getSheetAt(0);
HSSFRow row=sheet.getRow(0);
HSSFComment fidComment=null;
HSSFComment errorComment=null;
fidComment=null;
for(int i=0;i<row.getLastCellNum();i++)
{
if(row.getCell(i)==null)continue;
HSSFComment tmpcomment=row.getCell(i).getCellComment();
if(tmpcomment!=null&&tmpcomment.getString().getString().equals("fid"))
{
fidComment=tmpcomment;
}
if(tmpcomment!=null&&tmpcomment.getString().getString().equals("errorMsg"))
{
errorComment=tmpcomment;
}
}
HSSFPatriarch patr = sheet.getDrawingPatriarch();//sheet.createDrawingPatriarch();
if(patr==null)patr = sheet.createDrawingPatriarch();
System.out.println("------------------"+patr);
if(fidComment==null)
{
//没有就创建
HSSFCell cell=row.createCell(row.getLastCellNum());
System.out.println("=============="+row.getLastCellNum());
// 添加单元格注释
// 定义注释的大小和位置,详见文档
fidComment = patr.createComment( new HSSFClientAnchor( 0 , 0 , 0 , 0 , ( short ) 4 , 2 , ( short ) 6 , 5 ));
// 设置注释内容
fidComment.setString( new HSSFRichTextString( "fid" ));
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
fidComment.setAuthor( "system" );
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("ABCDEFG-HIJK-eeeeeee");
cell.setCellComment(fidComment);
}
boolean sec=true;
System.out.println("------------------"+errorComment);
if(sec&&errorComment==null)
{
HSSFCell cell=row.createCell(row.getLastCellNum());
System.out.println("=============="+row.getLastCellNum());
// 添加单元格注释
// 定义注释的大小和位置,详见文档
errorComment = patr.createComment( new HSSFClientAnchor( 0 , 0 , 0 , 0 , ( short ) 4 , 2 , ( short ) 6 , 5 ));
// 设置注释内容
errorComment.setString( new HSSFRichTextString( "errorMsg" ));
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
errorComment.setAuthor( "system" );
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("bbbb");
cell.setCellComment(errorComment);
}
//输出
try {
FileOutputStream fileOut = new FileOutputStream( "E:/excelImpTest/testData.xls" );
dataBook.write(fileOut);
//fileOut.flush();
fileOut.close();
} catch (Exception e) {
System.out.println(e.toString());
} }
public static void main(String[] args) {
// TODO Auto-generated method stub
tttt t=new tttt();
t.testExcel();
}
}
public class tttt {
//创建excel批注测试
public void testExcel()
{
HSSFWorkbook dataBook=null;
try{
FileInputStream fis = new FileInputStream("E:/excelImpTest/testData.xls");
POIFSFileSystem poiFs = new POIFSFileSystem(fis);
dataBook=new HSSFWorkbook(poiFs);
}catch(Exception e)
{
e.printStackTrace();
}
HSSFSheet sheet=dataBook.getSheetAt(0);
HSSFRow row=sheet.getRow(0);
HSSFComment fidComment=null;
HSSFComment errorComment=null;
fidComment=null;
for(int i=0;i<row.getLastCellNum();i++)
{
if(row.getCell(i)==null)continue;
HSSFComment tmpcomment=row.getCell(i).getCellComment();
if(tmpcomment!=null&&tmpcomment.getString().getString().equals("fid"))
{
fidComment=tmpcomment;
}
if(tmpcomment!=null&&tmpcomment.getString().getString().equals("errorMsg"))
{
errorComment=tmpcomment;
}
}
HSSFPatriarch patr = sheet.getDrawingPatriarch();//sheet.createDrawingPatriarch();
if(patr==null)patr = sheet.createDrawingPatriarch();
System.out.println("------------------"+patr);
if(fidComment==null)
{
//没有就创建
HSSFCell cell=row.createCell(row.getLastCellNum());
System.out.println("=============="+row.getLastCellNum());
// 添加单元格注释
// 定义注释的大小和位置,详见文档
fidComment = patr.createComment( new HSSFClientAnchor( 0 , 0 , 0 , 0 , ( short ) 4 , 2 , ( short ) 6 , 5 ));
// 设置注释内容
fidComment.setString( new HSSFRichTextString( "fid" ));
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
fidComment.setAuthor( "system" );
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("ABCDEFG-HIJK-eeeeeee");
cell.setCellComment(fidComment);
}
boolean sec=true;
System.out.println("------------------"+errorComment);
if(sec&&errorComment==null)
{
HSSFCell cell=row.createCell(row.getLastCellNum());
System.out.println("=============="+row.getLastCellNum());
// 添加单元格注释
// 定义注释的大小和位置,详见文档
errorComment = patr.createComment( new HSSFClientAnchor( 0 , 0 , 0 , 0 , ( short ) 4 , 2 , ( short ) 6 , 5 ));
// 设置注释内容
errorComment.setString( new HSSFRichTextString( "errorMsg" ));
// 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容.
errorComment.setAuthor( "system" );
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("bbbb");
cell.setCellComment(errorComment);
}
//输出
try {
FileOutputStream fileOut = new FileOutputStream( "E:/excelImpTest/testData.xls" );
dataBook.write(fileOut);
//fileOut.flush();
fileOut.close();
} catch (Exception e) {
System.out.println(e.toString());
} }
public static void main(String[] args) {
// TODO Auto-generated method stub
tttt t=new tttt();
t.testExcel();
}
}
是运行程序2次后, 再在资源管理器中打开刚才那excel文件,就会报 文件错误,数据可能丢失