- 用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类型的,所以直接用构造方法创建单元格行不通,只能通过行来创建单元格。
- 创建单元格
使用方法如下:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short)2);
- 读取现有单元格
使用方法如下:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)2);
- 行里创建单元格
public HSSFCell createCell(short column):
(short column)创建指定列号的单元格。列号和行号一样,也是从0开始数的。创建的单元格以「HSSFCell」类的对象返回,关于「HSSFCell」类的介绍,我们放在下面几章进行。
package linkin;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Linkin
{
public static void main(String[] args)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(1);//创建序号为1的行,第2行
HSSFCell cell = row.createCell(2);//创建序号为2的单元格,第二行第3格
cell.setCellValue("test");//写入test
FileOutputStream out = null;
try
{
out = new FileOutputStream("sample.xls");
workbook.write(out);
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
out.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
- 读取单元格
public HSSFCell getCell(short cellnum):
(short cellnum)取得指定列号的单元格。如果是不存在的单元格,会返回「null」。
package linkin;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Linkin
{
public static void main(String[] args)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(1);//创建第二行
HSSFCell cell = row.createCell((short) 2);//创建第二行第三格
cell.setCellValue("test");//第二行第三格写入test
for (int i = 0; i < 3; i++)
{
HSSFCell c = row.getCell((short) i);
if (c == null)
{
System.out.println("第" + i + "列单元格不存在");
}
else
{
System.out.println("第" + i + "列单元格获取成功");
}
}
}
}
- 读取有值的Excel表格
package linkin;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class Linkin
{
public static void main(String[] args)
{
FileInputStream in = null;
HSSFWorkbook workbook = null;
try
{
in = new FileInputStream("sample.xls");
POIFSFileSystem fs = new POIFSFileSystem(in);
workbook = new HSSFWorkbook(fs);
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
HSSFSheet sheet = workbook.getSheetAt(0);//取得第一张sheet
HSSFRow row = sheet.getRow(1);//第2行
for (int i = 0; i < 3; i++)
{
HSSFCell c = row.getCell((short) i);
if (c == null)
{
System.out.println("第" + i + "列单元格不存