ITeye博客

利用POI实现简单的Excel读写操作

ryan_ncu · 2007-11-07 18:19 · JAVA · Apache · Excel · Markdown

利用POI实现简单的Excel读写操作

java 代码

  1. package test;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import org.apache.poi.hssf.usermodel.HSSFCell;
  5. import org.apache.poi.hssf.usermodel.HSSFRow;
  6. import org.apache.poi.hssf.usermodel.HSSFSheet;
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  9. public class POIExcelService
  10. {
  11. private POIFSFileSystem fileSystem;
  12. private HSSFWorkbook workbook;
  13. private HSSFSheet sheet;
  14. private HSSFRow row;
  15. private HSSFCell cell;
  16. public POIExcelService(){}
  17. /**
  18. *
  19. * @param xlsPath
  20. */
  21. public POIExcelService(String xlsPath) throws Exception
  22. {
  23. this.fileSystem = new POIFSFileSystem(new FileInputStream(xlsPath));
  24. this.workbook = new HSSFWorkbook(fileSystem);
  25. }
  26. /**
  27. * 获取单元格数据,以String格式返回
  28. * @param sheet
  29. * @param row
  30. * @param cell
  31. * @return
  32. */
  33. public String getCellValue(int sheet,int row,int cell)
  34. {
  35. setSheet(sheet);
  36. setRow(row);
  37. setCell(cell);
  38. return this.getCell().getStringCellValue();
  39. }
  40. //导出文件
  41. public void Export(String xlsPath, int sheet, int row, int cell, String value) throws Exception
  42. {
  43. FileOutputStream fileOut = new FileOutputStream(xlsPath);
  44. this.setSheet(sheet);
  45. this.setRow(row);
  46. this.setCell(cell);
  47. this.getCell().setCellValue(value);
  48. this.workbook.write(fileOut);
  49. fileOut.close();
  50. }
  51. public HSSFCell getCell() {
  52. return cell;
  53. }
  54. public void setCell(int cell) {
  55. this.cell = this.row.getCell((short)(cell-1));
  56. }
  57. public HSSFRow getRow() {
  58. return row;
  59. }
  60. public void setRow(int row) {
  61. this.row = this.sheet.getRow(row-1);
  62. }
  63. public HSSFSheet getSheet() {
  64. return sheet;
  65. }
  66. public void setSheet(int sheet) {
  67. //默认是从0开始的,这里利用n-1转换下,用1开始,调用好理解点
  68. this.sheet = this.workbook.getSheetAt(sheet-1);
  69. }
  70. /*
  71. public String getCellStringValue(HSSFCell cell) {
  72. String cellValue = "";
  73. switch (cell.getCellType()) {
  74. case HSSFCell.CELL_TYPE_STRING:
  75. cellValue = cell.getStringCellValue();
  76. if(cellValue.trim().equals("")||cellValue.trim().length()<=0)
  77. cellValue=" ";
  78. break;
  79. case HSSFCell.CELL_TYPE_NUMERIC:
  80. cellValue = String.valueOf(cell.getNumericCellValue());
  81. break;
  82. case HSSFCell.CELL_TYPE_FORMULA:
  83. cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
  84. cellValue = String.valueOf(cell.getNumericCellValue());
  85. break;
  86. case HSSFCell.CELL_TYPE_BLANK:
  87. cellValue=" ";
  88. break;
  89. case HSSFCell.CELL_TYPE_BOOLEAN:
  90. break;
  91. case HSSFCell.CELL_TYPE_ERROR:
  92. break;
  93. default:
  94. break;
  95. }
  96. return cellValue;
  97. }
  98. */
  99. public static void main(String[] args)
  100. {
  101. try {
  102. POIExcelService excelOper = new POIExcelService("产品项目导入模板.xls");
  103. String tmp = excelOper.getCellValue(1, 1, 1);
  104. System.out.println(tmp);
  105. excelOper.Export("产品项目导入模板_导出.xls",1,1,1,"ABCDE");
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。