POI--HSSFCell类

  • 用POI创建单元格,使用「HSSFCell」类

该类包含三个构造方法。 
protected HSSFCell(Workbook book, Sheet sheet, int row, CellValueRecordInterface cval)
protected HSSFCell(Workbook book, Sheet sheet, int row, short col)
protected HSSFCell(Workbook book, Sheet sheet, int row, short col, int type)
同之前一样,虽然有三个构造方法,但都是protected类型的,所以直接用构造方法创建单元格行不通,只能通过行来创建单元格。 

 

  • 创建单元格

在一个工作表里创建一个单元格,必须用「HSSFRow」类的「createCell」方法。 
使用方法如下: 

 
  1. HSSFWorkbook workbook = new HSSFWorkbook();

  2. HSSFSheet sheet = workbook.createSheet();

  3. HSSFRow row = sheet.createRow(0);

  4. HSSFCell cell = row.createCell((short)2);

 

  • 读取现有单元格

要读取某一行现有的单元格,使用「HSSFRow」类的「getCell」方法』。
使用方法如下: 

 
  1. HSSFWorkbook workbook = new HSSFWorkbook();

  2. HSSFSheet sheet = workbook.createSheet();

  3. HSSFRow row = sheet.getRow(0);

  4. HSSFCell cell = row.getCell((short)2);

 

  • 行里创建单元格

下面来看看如何在取得现有行或者在新创建的行里,再创建一个单元格。用POI来创建的话,使用「HSSFRow」类的「createCell」方法。 
public HSSFCell createCell(short column):
(short column)创建指定列号的单元格。列号和行号一样,也是从0开始数的。创建的单元格以「HSSFCell」类的对象返回,关于「HSSFCell」类的介绍,我们放在下面几章进行。 

 
  1. package linkin;

  2.  
  3.  
  4. import java.io.FileOutputStream;

  5. import java.io.IOException;

  6.  
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCell;

  9. import org.apache.poi.hssf.usermodel.HSSFRow;

  10. import org.apache.poi.hssf.usermodel.HSSFSheet;

  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  12.  
  13.  
  14. public class Linkin

  15. {

  16. public static void main(String[] args)

  17. {

  18. HSSFWorkbook workbook = new HSSFWorkbook();

  19.  
  20.  
  21. HSSFSheet sheet = workbook.createSheet();

  22. HSSFRow row = sheet.createRow(1);//创建序号为1的行,第2行

  23.  
  24.  
  25. HSSFCell cell = row.createCell(2);//创建序号为2的单元格,第二行第3格

  26. cell.setCellValue("test");//写入test

  27.  
  28.  
  29. FileOutputStream out = null;

  30. try

  31. {

  32. out = new FileOutputStream("sample.xls");

  33. workbook.write(out);

  34. }

  35. catch (IOException e)

  36. {

  37. System.out.println(e.toString());

  38. }

  39. finally

  40. {

  41. try

  42. {

  43. out.close();

  44. }

  45. catch (IOException e)

  46. {

  47. System.out.println(e.toString());

  48. }

  49. }

  50. }

  51.  
  52.  
  53. }

 

  • 读取单元格

如果要从现有的行里取得某一个单元格,POI提供了「HSSFRow」类的「getCell」方法。 
public HSSFCell getCell(short cellnum):
(short cellnum)取得指定列号的单元格。如果是不存在的单元格,会返回「null」。 

 
  1. package linkin;

  2.  
  3.  
  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.  
  9.  
  10. public class Linkin

  11. {

  12. public static void main(String[] args)

  13. {

  14. HSSFWorkbook workbook = new HSSFWorkbook();

  15.  
  16.  
  17. HSSFSheet sheet = workbook.createSheet();

  18. HSSFRow row = sheet.createRow(1);//创建第二行

  19.  
  20.  
  21. HSSFCell cell = row.createCell((short) 2);//创建第二行第三格

  22. cell.setCellValue("test");//第二行第三格写入test

  23.  
  24.  
  25. for (int i = 0; i < 3; i++)

  26. {

  27. HSSFCell c = row.getCell((short) i);

  28. if (c == null)

  29. {

  30. System.out.println("第" + i + "列单元格不存在");

  31. }

  32. else

  33. {

  34. System.out.println("第" + i + "列单元格获取成功");

  35. }

  36. }

  37. }

  38.  
  39.  
  40. }

 

  • 读取有值的Excel表格
 
  1. package linkin;

  2.  
  3.  
  4. import java.io.FileInputStream;

  5. import java.io.IOException;

  6.  
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCell;

  9. import org.apache.poi.hssf.usermodel.HSSFRow;

  10. import org.apache.poi.hssf.usermodel.HSSFSheet;

  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  13.  
  14.  
  15. public class Linkin

  16. {

  17. public static void main(String[] args)

  18. {

  19. FileInputStream in = null;

  20. HSSFWorkbook workbook = null;

  21.  
  22.  
  23. try

  24. {

  25. in = new FileInputStream("sample.xls");

  26. POIFSFileSystem fs = new POIFSFileSystem(in);

  27. workbook = new HSSFWorkbook(fs);

  28. }

  29. catch (IOException e)

  30. {

  31. System.out.println(e.toString());

  32. }

  33. finally

  34. {

  35. try

  36. {

  37. in.close();

  38. }

  39. catch (IOException e)

  40. {

  41. System.out.println(e.toString());

  42. }

  43. }

  44.  
  45.  
  46. HSSFSheet sheet = workbook.getSheetAt(0);//取得第一张sheet

  47. HSSFRow row = sheet.getRow(1);//第2行

  48.  
  49.  
  50. for (int i = 0; i < 3; i++)

  51. {

  52. HSSFCell c = row.getCell((short) i);

  53. if (c == null)

  54. {

  55. System.out.println("第" + i + "列单元格不存在");

  56. }

  57. else

  58. {

  59. System.out.println("第" + i + "列单元格取得成功");

  60. System.out.println("单元格的值:" + c.getStringCellValue());//getStringCellValue()取得单元格的值

  61. }

  62. }

  63. }

  64.  
  65.  
  66. }


 

  • POI向单元格写入值

前面介绍了如何用POI获取单元格的值,这一节介绍如何用POI向单元格里设定值。和之前获取值一样,对于不同类型的值,写入的方法也是不同的。 
值                   方法
boolean setCellValue(boolean value)
date setCellValue(java.util.Calendar value)
date setCellValue(java.util.Date value)
numeric setCellValue(double value)
string setCellValue(java.lang.String value)
方法名称相同,但参数类型不同,这样的方法有5个。关于日期型的方法,有Calender型和Date型两种。 
当然,POI也可以写入Error值和计算式。 
值 方法
error value setCellErrorValue(byte value)
Formula setCellFormula(java.lang.String formula)
先统一看一下各种方法的Doc文档说明吧。 
setCellValue(boolean value)
写入boolean型使用「setCellValue(boolean value)」方法。 
1,public void setCellValue(boolean value)如果对象单元格不是boolean型的话,则自动转换为boolean型写入。 

2,public void setCellValue(java.util.Calendar value)写入Calendar型使用「setCellValue(java.util.Calendar value)」方法。 Calendar型的值是作为数值型写入的,如何想表示为日期型的话,需要做格式转换处理。如果对象单元格不是数值型的话,则自动转换为数值型写入。 

3,public void setCellValue(java.util.Date value)写入Date型使用「setCellValue(java.util.Date value)」方法。 Date型的值是作为数值型写入的,如何想表示为日期型的话,需要做格式转换处理。如果对象单元格不是数值型的话,则自动转换为数值型写入。 

4,public void setCellValue(double value)写入数值型使用「setCellValue(double value)」方法。 如果对象单元格不是数值型的话,则自动转换为数值型写入。 

5,public void setCellValue(java.lang.String value)写入字符串型使用「setCellValue(java.lang.String value)」方法。 注意:要写入中文等文字的话,必须要先使用「setEncoding」方法进行转换。如果对象单元格不是字符串型的话,则自动转换为字符串型写入。还有,如果写入的值是”null”的话,则单元格为空白。 

6,setCellErrorValuepublic void setCellErrorValue(byte value)写入Error型使用「setCellErrorValue(byte value)」方法。 如果对象单元格不是Error型的话,则自动转换为Error型写入。 

7,public void setCellFormula(java.lang.String formula)写入计算式使用「setCellFormula」方法。 写入计算式方法的参数虽然是字符串,但Doc文档里一点说明也没有。 

 
  1. package linkin;

  2.  
  3.  
  4. import java.io.FileOutputStream;

  5. import java.io.IOException;

  6. import java.util.Calendar;

  7. import java.util.Date;

  8.  
  9.  
  10. import org.apache.poi.hssf.usermodel.HSSFCell;

  11. import org.apache.poi.hssf.usermodel.HSSFErrorConstants;

  12. import org.apache.poi.hssf.usermodel.HSSFRow;

  13. import org.apache.poi.hssf.usermodel.HSSFSheet;

  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  15.  
  16.  
  17. public class Linkin

  18. {

  19. public static void main(String[] args)

  20. {

  21. HSSFWorkbook workbook = new HSSFWorkbook();

  22.  
  23.  
  24. HSSFSheet sheet = workbook.createSheet();

  25. HSSFRow row = sheet.createRow(1);//创建第二行

  26.  
  27.  
  28. HSSFCell cell1 = row.createCell((short) 0);//2,1格

  29. cell1.setCellValue(true);//写入true

  30.  
  31.  
  32. HSSFCell cell2 = row.createCell((short) 1);//2,2格

  33. Calendar cal = Calendar.getInstance();//Calendar???

  34. cell2.setCellValue(cal);//写入Calendar型对象cal

  35.  
  36.  
  37. HSSFCell cell3 = row.createCell((short) 2);//2,3格

  38. Date date = new Date(); //日期型

  39. cell3.setCellValue(date);//写入日期型

  40.  
  41.  
  42. HSSFCell cell4 = row.createCell((short) 3);//2,4格

  43. cell4.setCellValue(150);//写入150

  44.  
  45.  
  46. HSSFCell cell5 = row.createCell((short) 4);//2.5格

  47. cell5.setCellValue("hello");//写入hello

  48.  
  49.  
  50. HSSFRow row2 = sheet.createRow(2);//第三行

  51.  
  52.  
  53. HSSFCell cell6 = row2.createCell((short) 0);//3,1格

  54.  
  55.  
  56. FileOutputStream out = null;

  57. try

  58. {

  59. out = new FileOutputStream("sample.xls");

  60. workbook.write(out);

  61. }

  62. catch (IOException e)

  63. {

  64. System.out.println(e.toString());

  65. }

  66. finally

  67. {

  68. try

  69. {

  70. out.close();

  71. }

  72. catch (IOException e)

  73. {

  74. System.out.println(e.toString());

  75. }

  76. }

  77. }

  78.  
  79.  
  80. }


 

  • POI获取单元格类值类型

在获取单元格值的时候,不同类型,使用的方法也不一样,所以获取之前,必须要知道类型。这一节就来介绍如何知道单元格里的值是何类型。 
要获取单元格值的类型,使用「HSSFCell」类的「getCellType」方法。 注意:数值型和日期型都是「CELL_TYPE_NUMERIC 
下面就是类型和定义值的一览表。 
Cell type 定义值 值(int)
Blank CELL_TYPE_BLANK 3
Boolean CELL_TYPE_BOOLEAN 4
Error CELL_TYPE_ERROR 5
Formula CELL_TYPE_FORMULA 2
Numeric CELL_TYPE_NUMERIC 0

String CELL_TYPE_STRING 1

POI--HSSFCell类_第1张图片

 
  1. package linkin;

  2.  
  3.  
  4. import java.io.FileInputStream;

  5. import java.io.IOException;

  6.  
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCell;

  9. import org.apache.poi.hssf.usermodel.HSSFRow;

  10. import org.apache.poi.hssf.usermodel.HSSFSheet;

  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  13.  
  14.  
  15. public class Linkin

  16. {

  17. public static void main(String[] args)

  18. {

  19. FileInputStream in = null;

  20. HSSFWorkbook workbook = null;

  21.  
  22.  
  23. try

  24. {

  25. in = new FileInputStream("sample.xls");

  26. POIFSFileSystem fs = new POIFSFileSystem(in);

  27. workbook = new HSSFWorkbook(fs);

  28. }

  29. catch (IOException e)

  30. {

  31. System.out.println(e.toString());

  32. }

  33. finally

  34. {

  35. try

  36. {

  37. in.close();

  38. }

  39. catch (IOException e)

  40. {

  41. System.out.println(e.toString());

  42. }

  43. }

  44.  
  45.  
  46. HSSFSheet sheet = workbook.getSheetAt(0);

  47. HSSFRow row = sheet.getRow(1);

  48.  
  49.  
  50. HSSFCell cell = row.getCell((short) 0);

  51. System.out.println("A:2=" + getType(cell.getCellType()));

  52.  
  53.  
  54. cell = row.getCell((short) 1);

  55. System.out.println("B:2=" + getType(cell.getCellType()));

  56.  
  57.  
  58. cell = row.getCell((short) 2);

  59. System.out.println("C:2=" + getType(cell.getCellType()));

  60.  
  61.  
  62. cell = row.getCell((short) 3);

  63. System.out.println("D:2=" + getType(cell.getCellType()));

  64.  
  65.  
  66. cell = row.getCell((short) 4);

  67. System.out.println("E:2=" + getType(cell.getCellType()));

  68. }

  69.  
  70.  
  71. public static String getType(int type)

  72. {

  73. if (type == HSSFCell.CELL_TYPE_BLANK)

  74. {

  75. return "CELL_TYPE_BLANK";

  76. }

  77. else if (type == HSSFCell.CELL_TYPE_BOOLEAN)

  78. {

  79. return "CELL_TYPE_BOOLEAN";

  80. }

  81. else if (type == HSSFCell.CELL_TYPE_ERROR)

  82. {

  83. return "CELL_TYPE_ERROR";

  84. }

  85. else if (type == HSSFCell.CELL_TYPE_FORMULA)

  86. {

  87. return "CELL_TYPE_FORMULA";

  88. }

  89. else if (type == HSSFCell.CELL_TYPE_NUMERIC)

  90. {

  91. return "CELL_TYPE_NUMERIC";

  92. }

  93. else if (type == HSSFCell.CELL_TYPE_STRING)

  94. {

  95. return "CELL_TYPE_STRING";

  96. }

  97. else

  98. {

  99. return "Not defined";

  100. }

  101. }

  102.  
  103.  
  104. }



 

  • 日期型检查

到底是数值型还是日期型,可以通过「org.apache.poi.hssf.usermodel.HSSFDateUtil」类的「isCellDateFormatted」方法来检证。 
public static boolean isCellDateFormatted(HSSFCell cell)

因为这个方法是静态的,可以直接调用如下所示。 

 

 
  1. if (HSSFDateUtil.isCellDateFormatted(cell)){

  2. /* 日期型 */

  3. }

 

 
  1. package linkin;

  2.  
  3.  
  4. import java.io.FileInputStream;

  5. import java.io.IOException;

  6.  
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCell;

  9. import org.apache.poi.hssf.usermodel.HSSFDateUtil;

  10. import org.apache.poi.hssf.usermodel.HSSFRow;

  11. import org.apache.poi.hssf.usermodel.HSSFSheet;

  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  13. import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  14.  
  15.  
  16. public class Linkin

  17. {

  18. public static void main(String[] args)

  19. {

  20. FileInputStream in = null;

  21. HSSFWorkbook workbook = null;

  22.  
  23.  
  24. try

  25. {

  26. in = new FileInputStream("sample.xls");

  27. POIFSFileSystem fs = new POIFSFileSystem(in);

  28. workbook = new HSSFWorkbook(fs);

  29. }

  30. catch (IOException e)

  31. {

  32. System.out.println(e.toString());

  33. }

  34. finally

  35. {

  36. try

  37. {

  38. in.close();

  39. }

  40. catch (IOException e)

  41. {

  42. System.out.println(e.toString());

  43. }

  44. }

  45.  
  46.  
  47. HSSFSheet sheet = workbook.getSheetAt(0);

  48. HSSFRow row = sheet.getRow(1);

  49.  
  50.  
  51. HSSFCell cell = row.getCell((short) 0);

  52. System.out.println("A:2=" + getType(cell));

  53.  
  54.  
  55. cell = row.getCell((short) 1);

  56. System.out.println("B:2=" + getType(cell));

  57.  
  58.  
  59. cell = row.getCell((short) 2);

  60. System.out.println("C:2=" + getType(cell));

  61.  
  62.  
  63. cell = row.getCell((short) 3);

  64. System.out.println("D:2=" + getType(cell));

  65.  
  66.  
  67. cell = row.getCell((short) 4);

  68. System.out.println("E:2=" + getType(cell));

  69. }

  70.  
  71.  
  72. public static String getType(HSSFCell cell)

  73. {

  74. int type = cell.getCellType();

  75.  
  76.  
  77. if (type == HSSFCell.CELL_TYPE_BLANK)

  78. {

  79. return "CELL_TYPE_BLANK";

  80. }

  81. else if (type == HSSFCell.CELL_TYPE_BOOLEAN)

  82. {

  83. return "CELL_TYPE_BOOLEAN";

  84. }

  85. else if (type == HSSFCell.CELL_TYPE_ERROR)

  86. {

  87. return "CELL_TYPE_ERROR";

  88. }

  89. else if (type == HSSFCell.CELL_TYPE_FORMULA)

  90. {

  91. return "CELL_TYPE_FORMULA";

  92. }

  93. else if (type == HSSFCell.CELL_TYPE_NUMERIC)

  94. {

  95. //检查日期类型

  96. if (HSSFDateUtil.isCellDateFormatted(cell))

  97. {

  98. return "CELL_TYPE_DATE";

  99. }

  100. else

  101. {

  102. return "CELL_TYPE_NUMERIC";

  103. }

  104. }

  105. else if (type == HSSFCell.CELL_TYPE_STRING)

  106. {

  107. return "CELL_TYPE_STRING";

  108. }

  109. else

  110. {

  111. return "Not defined";

  112. }

  113. }

  114. }

 

  • POI获取单元格类的值

这一节介绍如何获取单元格的值。在POI里,对于不同类型的单元格的值,所调用的方法是不一样的。 
型 函数名
boolean型 getBooleanCellValue()
java.util.Date型 getDateCellValue()
byte型 getErrorCellValue()
double型 getNumericCellValue()
java.lang.String型 getStringCellValue()
java.lang.String型 getCellFormula()
如果单元格里是String型的值,调用方法「getStringCellValue」,数值型的话,则调用「getNumericCellValue」。再一起来看看其它方法。 
 

1,public boolean getBooleanCellValue()获取boolean型的値,调用「getBooleanCellValue」方法。如果调用这个方法来获取字符串,数值型的值的话,会发生Exception异常,如果对象单元格里不含值,则返回「false」。 

2,public java.util.Date getDateCellValue()获取日期Date型时,调用「getDateCellValue」方法。 如果用这个方法来获取字符串时,会有Exception异常发生,如果对象单元格里不含值,则返回「null」。 

3,public byte getErrorCellValue()单元格如果是Error值,比如#VALUE!等,这时可以使用「getErrorCellValue」方法。 字符串,数值型,布尔型的值用这个方法的话,会产生异常,而单元格里值为空时则返回「0」。 
对于单元格的Error值类型,在POI里的「org.apache.poi.hssf.usermodel.HSSFErrorConstants」有定义列表如下: 
常量名 错误代码 Excel里的Error值
public static final byte ERROR_DIV0 7 #DIV/0!
public static final byte ERROR_NA 42 #N/A
public static final byte ERROR_NAME 29 #NAME?
public static final byte ERROR_NULL 0 #NULL!
public static final byte ERROR_NUM 36 #NUM!
public static final byte ERROR_REF 23 #REF!
public static final byte ERROR_VALUE 15 #VALUE!
所以这里要注意了,Excel的ERROR.TYPE函数所返回的值,和POI所定义的是不一样的。 
4,public double getNumericCellValue()读取数值型时,调用「getNumericCellValue」方法。 用这个方法来读取字符串类型时,会有Exception发生,如果单元格里值为空时则返回「0」。 

5,public java.lang.String getStringCellValue()字符串类型取得时,使用「getStringCellValue」方法。 同样的,用这个方法来读取数值型时,也会有异常Exception发生。单元格值为空时,则返回空白。 

6,public java.lang.String getCellFormula()读取单元格里的计算式时,使用「getCellFormula」方法。单元格里如果是计算式的话,则把计算式作为字符串来获取。关于具体内容,在最新文档里,竟然没有,不知道为什么。 

 
  1. package linkin;

  2.  
  3.  
  4. import java.io.FileInputStream;

  5. import java.io.IOException;

  6.  
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCell;

  9. import org.apache.poi.hssf.usermodel.HSSFRow;

  10. import org.apache.poi.hssf.usermodel.HSSFSheet;

  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  13.  
  14.  
  15. public class Linkin

  16. {

  17. public static void main(String[] args)

  18. {

  19. FileInputStream in = null;

  20. HSSFWorkbook workbook = null;

  21.  
  22.  
  23. try

  24. {

  25. in = new FileInputStream("sample.xls");

  26. POIFSFileSystem fs = new POIFSFileSystem(in);

  27. workbook = new HSSFWorkbook(fs);

  28. }

  29. catch (IOException e)

  30. {

  31. System.out.println(e.toString());

  32. }

  33. finally

  34. {

  35. try

  36. {

  37. in.close();

  38. }

  39. catch (IOException e)

  40. {

  41. System.out.println(e.toString());

  42. }

  43. }

  44.  
  45.  
  46. HSSFSheet sheet = workbook.getSheetAt(0);

  47. HSSFRow row = sheet.getRow(1);

  48.  
  49.  
  50. HSSFCell cell = row.getCell((short) 0);

  51. System.out.println(cell.getNumericCellValue());

  52.  
  53.  
  54. cell = row.getCell((short) 1);

  55. System.out.println(cell.getStringCellValue());

  56.  
  57.  
  58. cell = row.getCell((short) 2);

  59. System.out.println(cell.getBooleanCellValue());

  60.  
  61.  
  62. cell = row.getCell((short) 3);

  63. System.out.println(cell.getErrorCellValue());

  64.  
  65.  
  66. cell = row.getCell((short) 4);

  67. System.out.println(cell.getDateCellValue());

  68. }

  69. }

 

  • POI获取行单元格相关信息

有时可能你想要知道行里面单元格的具体信息,比方说,该行实际上有多少单元格,最初的单元格在什么位置,最后的单元格又在哪里?用POI也可以实现。
要取得某一行实际存在的单元格的总数,使用「HSSFRow」类的「getPhysicalNumberOfCells」方法。 

1,public int getPhysicalNumberOfCells()要想知道最初的单元格的位置,可以使用「HSSFRow」类的「getFirstCellNum」方法。 

2,public short getFirstCellNum()你也可以使用「HSSFRow」类的「getLastCellNum」方法来获取最后的单元格的位置,但这里要注意一点,如果单元格存在,如下面的API文档所说,获取的值是实际列号再加上1,在以前的版本可不是这样的。 

3,public short getLastCellNum()需要注意的是,POI在获取最初单元格位置和最后单元格位置时,返回的值并不是某一行特有的,而是针对所有的行。也就是说,上面的两个方法,针对所有的行,返回一个最小的列号和最大的列号。因此,不管对于哪一行,实行上面的两个方法,返回的值都是一样的。 

 
  1. package linkin;

  2.  
  3.  
  4. import org.apache.poi.hssf.usermodel.HSSFRow;

  5. import org.apache.poi.hssf.usermodel.HSSFSheet;

  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  7.  
  8.  
  9. public class Linkin

  10. {

  11. public static void main(String[] args)

  12. {

  13. HSSFWorkbook workbook = new HSSFWorkbook();

  14.  
  15.  
  16. HSSFSheet sheet = workbook.createSheet();

  17. HSSFRow row = sheet.createRow(1);

  18.  
  19.  
  20. System.out.println("创建单元格前的状态:");

  21. System.out.println("First:" + row.getFirstCellNum());

  22. System.out.println("Last:" + row.getLastCellNum());

  23. System.out.println("Total:" + row.getPhysicalNumberOfCells() + "\n");

  24.  
  25.  
  26. row.createCell((short) 1);

  27.  
  28.  
  29. System.out.println("创建第二列(列号为1)单元格:");

  30. System.out.println("First:" + row.getFirstCellNum());

  31. System.out.println("Last:" + row.getLastCellNum());

  32. System.out.println("Total:" + row.getPhysicalNumberOfCells() + "\n");

  33.  
  34.  
  35. row.createCell((short) 3);

  36.  
  37.  
  38. System.out.println("创建第四列(列号为3)单元格:");

  39. System.out.println("First:" + row.getFirstCellNum());

  40. System.out.println("Last:" + row.getLastCellNum());

  41. System.out.println("Total:" + row.getPhysicalNumberOfCells() + "\n");

  42. }

  43. }


一行当中没有单元格的情况下,调用「getFirstCellNum」方法和「getLastCellNum」方法,返回值都是「-1」。有单元格时,调用「getLastCellNum」方法,返回的值总是实际列号再加上1(实际第几列)。 

 

  • 从现有的Excel文件读取单元格信息
 
  1. package linkin;

  2.  
  3.  
  4. import java.io.FileInputStream;

  5. import java.io.IOException;

  6.  
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFRow;

  9. import org.apache.poi.hssf.usermodel.HSSFSheet;

  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  11. import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  12.  
  13.  
  14. public class Linkin

  15. {

  16. public static void main(String[] args)

  17. {

  18. FileInputStream in = null;

  19. HSSFWorkbook workbook = null;

  20.  
  21.  
  22. try

  23. {

  24. in = new FileInputStream("sample.xls");

  25. POIFSFileSystem fs = new POIFSFileSystem(in);

  26. workbook = new HSSFWorkbook(fs);

  27. }

  28. catch (IOException e)

  29. {

  30. System.out.println(e.toString());

  31. }

  32. finally

  33. {

  34. try

  35. {

  36. in.close();

  37. }

  38. catch (IOException e)

  39. {

  40. System.out.println(e.toString());

  41. }

  42. }

  43.  
  44.  
  45. HSSFSheet sheet = workbook.getSheetAt(0);

  46. HSSFRow row = sheet.getRow(1);

  47.  
  48.  
  49. System.out.println("First:" + row.getFirstCellNum());

  50. System.out.println("Last:" + row.getLastCellNum());

  51. System.out.println("Total:" + row.getPhysicalNumberOfCells() + "\n");

  52.  
  53.  
  54. row = sheet.getRow(2);

  55.  
  56.  
  57. System.out.println("First:" + row.getFirstCellNum());

  58. System.out.println("Last:" + row.getLastCellNum());

  59. System.out.println("Total:" + row.getPhysicalNumberOfCells() + "\n");

  60. }

  61. }

你可能感兴趣的:(solr与mq与poi)