close
※怕自己忘記特別做個簡單的操作紀錄※
目前是使用Array做暫存資料的動作,之後透過呼叫write的function去做寫入新的Excel的動作
特別需要注意一下Row跟Cell的位置關係以及資料愈放置的位置
public class POITester {
static XSSFWorkbook wb = null;
static XSSFSheet sheet = null;
static XSSFRow row = null;
static XSSFCell cell = null;
static ArrayList<String> data = new ArrayList();
/**
* 读取文件指定行。
*/
public static void main(String[] args) throws IOException {
scaneWholeFile(".\\xxxxxxx.xlsx");
writeToExcel();
}
public static void scaneWholeFile(String file) throws FileNotFoundException, IOException {
//1.Read Excel File into workbook
FileInputStream inp = new FileInputStream(file);
wb = new XSSFWorkbook(inp);
inp.close();
//2.get wb sheet(0)
sheet = wb.getSheetAt(0);
//this loop will scane all info at cell
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
//get each row
XSSFRow row1 = sheet.getRow(i);
//get each cols
XSSFCell cell1 = row1.getCell(2);
//特別需要將Cell1中的值透過toString轉成字串才可以顯示正確資料
data.add(cell1.toString());
}
}
private static void writeToExcel() throws FileNotFoundException, IOException {
XSSFWorkbook writewb = new XSSFWorkbook();
XSSFSheet writeSheet = writewb.createSheet("Test");
for (int i = 0; i < sheet.getLastRowNum(); i++) {
XSSFRow writeRow = writeSheet.createRow(i);
writeRow.createCell(0).setCellValue(data.get(i));
}
try (FileOutputStream out = new FileOutputStream(".\\xxxxxxx.xlsx")) {
writewb.write(out);
}
}
}
全站熱搜
留言列表

