package apiTest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import org.apache.poi.hssf.extractor.ExcelExtractor;
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;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.hssf.util.*;
import java.io.*;
public class ReadExcel {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
HSSFWorkbook wb = null;
POIFSFileSystem fs = null;
try {
fs = new POIFSFileSystem(new FileInputStream("e://workbook.xls"));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
HSSFSheet sheet = wb.getSheet("Sheet1");
//HSSFSheet sheet = wb.getSheetAt(0);
int cellLast = 0;
int rows = sheet.getLastRowNum()+1;
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (row != null) {
int cells = row.getLastCellNum();
if (cells > cellLast) {
cellLast = cells;
}
}
}
int count = rows>cellLast ? rows:cellLast;
for(int i = 0 ; i< count; i++) {
//System.out.println(sheet.getMergedRegion(i));
}
HSSFRow row2 = sheet.getRow(1);//0 start
HSSFCell cell2 = row2.getCell(1); //0 start
System.out.println(cell2);
// HSSFRow row3 = sheet.getRow(1);
// HSSFCell cell3 = row3.getCell(2);
// System.out.println(cell3);
HSSFRow row1 = sheet.createRow(1); //0 start
HSSFCell cell1 = row1.createCell(2); //1 start
cell1.setCellValue("fuck1");
HSSFRow row12 = sheet.createRow(1); //0 start
HSSFCell cell12 = row12.createCell(3); //1 start
cell12.setCellValue("fuck3");
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
//sheet.addMergedRegion(new Region(1,(short)3,1,(short)3));
FileOutputStream fileOut = new FileOutputStream( "e://workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}