用POI在工作表里作成一个行,可以用「HSSFRow」类,它的构造方法有三个。
protected HSSFRow()
protected HSSFRow(Workbook book, Sheet sheet, int rowNum)Creates new HSSFRow from scratch.
protected HSSFRow(Workbook book, Sheet sheet, RowRecord record)Creates an HSSFRow from a low level RowRecord object.
虽然有三个,但每一个都是protected的,所以不能直接使用它的构造方法,而是用Sheet对象来创建行。
- 创建行
使用方法如下:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0);
- 取得已经存在的行
使用方法如下:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.getRow(0);
- 获取行的状态
我们还可以用POI获取当前工作表的行的一些状态.
获得第一行的序列号,使用「HSSFSheet」类的「getFirstRowNum」方法。
获得最后一行的序列号,使用「HSSFSheet」类的「getLastRowNum」方法。
获得实际存在的行的总数,使用「HSSFSheet」类的「getPhysicalNumberOfRows」方法。
代码如下:
package linkin;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class Linkin
{
public static void main(String[] args)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
System.out.println("创建行之前的状态:");
System.out.println("First:" + sheet.getFirstRowNum());//sheet.getFirstRowNum()获取sheet的第一行行号
System.out.println("Last:" + sheet.getLastRowNum());//getLastRowNum()获取sheet的最后行行号
System.out.println("Total:" + sheet.getPhysicalNumberOfRows() + "\n");// getPhysicalNumberOfRows()获取sheet的行总数
sheet.createRow(1);
System.out.println("创建第一行后的状态:");
System.out.println("First:" + sheet.getFirstRowNum());
System.out.println("Last:" + sheet.getLastRowNum());
System.out.println("Total:" + sheet.getPhysicalNumberOfRows() + "\n");
sheet.createRow(3);
System.out.println("创建第三行后的状态:");
System.out.println("First:" + sheet.getFirstRowNum());
System.out.println("Last:" + sheet.getLastRowNum());
System.out.println("Total:" + sheet.getPhysicalNumberOfRows());
}
}
- 行的创建
在Sheet里创建行,使用「HSSFSheet」类的「createRow」方法。
public HSSFRow createRow(int rownum)
(int rownum)创建指定行号的行。行号是从0开始的整数,最大是65535,可以支持65536行。创建行所返回的值是「HSSFRow」类对象,关于「HSSFRow」类定义的说明,以后再详细说。创建方法很多,要说详细挺复杂。比方说,即使第一行(行号为0)和第二行不创建,也能直接创建第三行。
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();//创建个空白的workbook
HSSFSheet sheet = workbook.createSheet();//创建个空白的sheet
HSSFRow row = sheet.createRow(2);//创建行号为2的行,excel中的第三行
HSSFCell cell = row.createCell((short)0);//创建上面行的第一个单元格
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 HSSFRow getRow(int rownum):利用行号取得指定的行。如果行不存在,则返回NULL。
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);
for (int i = 0; i < 3; i++)
{
HSSFRow r = sheet.getRow(i);
if (r == null)
{
System.out.println("第" + i + "行不存在。");
//第0行不存在。第1行取得成功。第2行不存在。
}
else
{
System.out.println("第" + i + "行取得成功。");
}
}
}
}
- 读取有值的行
package linkin;
import java.io.FileInputStream;
import java.io.IOException;
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);//读取序号为0的sheet
for (int i = 0; i < 3; i++)
{
HSSFRow r = sheet.getRow(i);
if (r == null)
{
System.out.println("第" + i + "行不存在。");
}
else
{
System.out.println("第" + i + "行取得成功。");
}
}
}
}
- 从取得的行里获取单元格的值。
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);//读取序号为0的sheet
HSSFRow row = sheet.getRow(2);//取得sheet中第二行(行号1)
HSSFCell cell = row.getCell((short) 0);//取得第二行,第二格(单元格号1)
System.out.println(cell.getStringCellValue());//cell.getStringCellValue()取值
}
}
- 移动行
关于这个方法有2个重载,所以这里多说几句:
public void shiftRows(int startRow, int endRow, int n)
int startRow, int endRow:指定要移动的行的范围从「startRow」行到「endRow」行。
int n:「n」如果是正数就往下移动的行数,如果为负,就往上移动。
public void shiftRows(int startRow, int endRow, int n,boolean copyRowHeight, boolean resetOriginalRowHeight)
如要使行高也一起移动的话,设置「copyRowHeight」参数为「true」。
还有,移动后,原来的行是保留原行高不变还是恢复到默认行高呢?可以设置「resetOriginalRowHeight」参数。为「true」时,则可以恢复到默认行高。反之则保留原行高不变。
package linkin;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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.shiftRows(1, 2, 2);//第二行和第三行向下移动两行
FileOutputStream out = null;
try
{
out = new FileOutputStream("sample2.xls");
workbook.write(out);
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
try
{
out.close();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}