修改excel文档示例
(2023-07-07 21:57:05)
标签:
poijava |
分类: J2EE笔记 |
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.ipan.kits.io.IOUtil;
import com.ipan.poi.excel.WorkbookFactory;
// 修改excel文档示例
public class ExcelTest {
public static void main(String[] args) {
Workbook wb = null;
FileInputStream fin = null;
FileOutputStream fout = null;
try {
File file = new File("e:/test/test.xlsx");
if (!file.exists()) return ;
fin = new FileInputStream(file);
wb = WorkbookFactory.create(fin);
Sheet sheet = wb.getSheetAt(1);
System.out.println(sheet.getLastRowNum());
Row dataRow = sheet.getRow(1);
if (dataRow == null) {
dataRow = sheet.createRow(1);
}
Cell cell = dataRow.createCell(1);
cell.setCellValue("测试");
// 必须写在后面,否则读取文件的字节会变成0;
fout = new FileOutputStream(file);
wb.write(fout);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtil.closeQuietly(fin);
IOUtil.closeQuietly(fout);
try {
if (wb != null) wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
前一篇:国内时间同步服务器