标签:poi
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()); } }
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()); } } } }
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()取值 } }
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()); } } } }
标签:poi
原文地址:http://blog.csdn.net/u011794238/article/details/46281181