---
title: "利用POI实现简单的Excel读写操作"
author: "ryan_ncu"
date: "2007-11-07T18:19:03Z"
tags: ["Apache", "Excel"]
slug: "poi-excel"
draft: false
lastmod: "2007-11-07T18:19:03Z"
id: 138838
url: "https://xkspace.iteye.com/blog/138838"
---

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

java 代码

001. package test;
002. import java.io.FileInputStream;
003. import java.io.FileOutputStream;
004. import org.apache.poi.hssf.usermodel.HSSFCell;
005. import org.apache.poi.hssf.usermodel.HSSFRow;
006. import org.apache.poi.hssf.usermodel.HSSFSheet;
007. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
008. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
009. public class POIExcelService
010. {
011. private POIFSFileSystem fileSystem;
012. private HSSFWorkbook workbook;
013. private HSSFSheet sheet;
014. private HSSFRow row;
015. private HSSFCell cell;
016. public POIExcelService(){}
017. /\**
018. \*
019. * @param xlsPath
020. \*/
021. public POIExcelService(String xlsPath) throws Exception
022. {
023. this.fileSystem = new POIFSFileSystem(new FileInputStream(xlsPath));
024. this.workbook = new HSSFWorkbook(fileSystem);
025. }
026. /\**
027. * 获取单元格数据,以String格式返回
028. * @param sheet
029. * @param row
030. * @param cell
031. * @return
032. \*/
033. public String getCellValue(int sheet,int row,int cell)
034. {
035. setSheet(sheet);
036. setRow(row);
037. setCell(cell);
038. return this.getCell().getStringCellValue();
039. }
040. //导出文件
041. public void Export(String xlsPath, int sheet, int row, int cell, String value) throws Exception
042. {
043. FileOutputStream fileOut = new FileOutputStream(xlsPath);
044. this.setSheet(sheet);
045. this.setRow(row);
046. this.setCell(cell);
047. this.getCell().setCellValue(value);
048. this.workbook.write(fileOut);
049. fileOut.close();
050. }
051. public HSSFCell getCell() {
052. return cell;
053. }
054. public void setCell(int cell) {
055. this.cell = this.row.getCell((short)(cell-1));
056. }
057. public HSSFRow getRow() {
058. return row;
059. }
060. public void setRow(int row) {
061. this.row = this.sheet.getRow(row-1);
062. }
063. public HSSFSheet getSheet() {
064. return sheet;
065. }
066. public void setSheet(int sheet) {
067. //默认是从0开始的,这里利用n-1转换下,用1开始,调用好理解点
068. this.sheet = this.workbook.getSheetAt(sheet-1);
069. }
070. /*
071. public String getCellStringValue(HSSFCell cell) {
072. String cellValue = "";
073. switch (cell.getCellType()) {
074. case HSSFCell.CELL\_TYPE\_STRING:
075. cellValue = cell.getStringCellValue();
076. if(cellValue.trim().equals("")||cellValue.trim().length()&lt;=0)
077. cellValue=" ";
078. break;
079. case HSSFCell.CELL\_TYPE\_NUMERIC:
080. cellValue = String.valueOf(cell.getNumericCellValue());
081. break;
082. case HSSFCell.CELL\_TYPE\_FORMULA:
083. cell.setCellType(HSSFCell.CELL\_TYPE\_NUMERIC);
084. cellValue = String.valueOf(cell.getNumericCellValue());
085. break;
086. case HSSFCell.CELL\_TYPE\_BLANK:
087. cellValue=" ";
088. break;
089. case HSSFCell.CELL\_TYPE\_BOOLEAN:
090. break;
091. case HSSFCell.CELL\_TYPE\_ERROR:
092. break;
093. default:
094. break;
095. }
096. return cellValue;
097. }
098. \*/
099. 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. }