1 package com.what21.test; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 import org.apache.poi.hssf.usermodel.HSSFCell; 10 import org.apache.poi.hssf.usermodel.HSSFRow; 11 import org.apache.poi.hssf.usermodel.HSSFSheet; 12 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 13 import org.apache.poi.xssf.usermodel.XSSFCell; 14 import org.apache.poi.xssf.usermodel.XSSFSheet; 15 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 16 17 public class TestPoi { 18 public static void updateExcel(File exlFile, String sheetName, int col, 19 int row, String value) throws Exception { 20 FileInputStream fis = new FileInputStream(exlFile); 21 HSSFWorkbook workbook = new HSSFWorkbook(fis); 22 // workbook. 23 HSSFSheet sheet = workbook.getSheet(sheetName); 24 HSSFCell mycell = sheet.createRow(row).createCell(col); 25 mycell.setCellValue(value); 26 HSSFRow r = sheet.getRow(row); 27 HSSFCell cell = r.getCell(col); 28 // int type=cell.getCellType(); 29 String str1 = cell.getStringCellValue(); 30 // 這裏假設對應單元格原來的類型也是String類型 31 cell.setCellValue(value); 32 System.out.println("單元格原來值爲" + str1); 33 System.out.println("單元格值被更新爲" + value); 34 35 fis.close();// 關閉文件輸入流 36 37 FileOutputStream fos = new FileOutputStream(exlFile); 38 workbook.write(fos); 39 fos.close();// 關閉文件輸出流 40 } 41 42 public static void update2(String url) { 43 int coloum = 2; // 好比你要獲取第1列 44 try { 45 HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(url)); 46 HSSFSheet sheet = workbook.getSheet("Sheet1"); 47 48 for (int i = 0; i <= sheet.getLastRowNum(); i++) { 49 HSSFRow row = sheet.getRow((short) i); 50 if (null == row) { 51 continue; 52 } else { 53 HSSFCell cell = row.getCell((short) coloum); 54 if (null == cell) { 55 continue; 56 } else { 57 58 cell.setCellValue("he1"); 59 } 60 } 61 } 62 FileOutputStream out = null; 63 try { 64 out = new FileOutputStream(url); 65 workbook.write(out); 66 } catch (IOException e) { 67 e.printStackTrace(); 68 } finally { 69 try { 70 out.close(); 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 } 75 } catch (FileNotFoundException e) { 76 e.printStackTrace(); 77 } catch (IOException e) { 78 e.printStackTrace(); 79 } 80 } 81 82 public static boolean writeXlsx(String fileName, int row, int column, 83 String content) { 84 boolean flag = false; 85 FileOutputStream out = null; 86 XSSFWorkbook xwb; 87 try { 88 xwb = new XSSFWorkbook(new FileInputStream(fileName)); 89 XSSFSheet xSheet = xwb.getSheetAt(0); 90 XSSFCell xCell = xSheet.createRow(row).createCell(column); 91 xCell.setCellValue(content); 92 out = new FileOutputStream(fileName); 93 xwb.write(out); 94 out.close(); 95 flag = true; 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } catch (RuntimeException e) { 99 e.printStackTrace(); 100 } 101 return flag; 102 } 103 104 public static void main(String[] args) throws Exception { 105 // TODO Auto-generated method stub 106 // 下面改爲你本身的xls文件進行測試,2003格式的,不能2007 107 File file = new File("C:\\Users\\Administrator\\Desktop\\test.xls"); 108 // 下面嘗試更改第一行第一列的單元格的值 109 updateExcel(file, "Sheet1", 0, 0, "hehe"); 110 update2("C:\\Users\\Administrator\\Desktop\\test.xls"); 111 File file1 = new File( 112 "C:\\Users\\Administrator\\Desktop\\test - 副本.xlsx"); 113 writeXlsx("C:\\Users\\Administrator\\Desktop\\test - 副本.xlsx", 0, 0, 114 "1"); 115 } 116 }
注意事項:若是修改的座標對應的單元格是空,會報錯。java
建議這麼寫:apache
HSSFCell mycell = sheet.createRow(row).createCell(col);
mycell.setCellValue(value);xss
這樣寫有一個問題:若是定位錯誤,會帶來沒必要要的麻煩。ide
一般在生成原始Excel時,最好能對空單元格賦空(強制添加空字符串),這樣在使用SAXPOI或者SAX解析較大文本的Excel的時候,能夠post
有效的避免由於空單元格而致使列數據錯位的問題。測試
上面的代碼是很常見的,在網上能找到一堆。根據不一樣的場景能夠傳入一組須要修改的座標,經過循環來修改。這樣能夠減小打開文件的次數,提升url
效率。spa