excel模板导入后追加内容
简单的导入表格,并追加内容.使适用于表头固定的excel,并不能动态生成表头.
新建java工程,导入poi jar包.
通过新建行和列往excel中追加内容.
package mjdai.testmodel; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * @author mjdai * @Discreption 根据已有的Excel模板,修改模板内容生成新Excel */ public class CreateExcel { public static void main(String[] args) throws IOException { // excel模板路径 D:\temp\excelTest File file = new File("D:/temp/excelTest/temple.xlsx"); // 读取excel模板 InputStream is = new FileInputStream(file); // File file = new FileInputStream(file) XSSFWorkbook wb = new XSSFWorkbook(is); // 读取了模板内所有sheet内容 XSSFSheet sheet1 = wb.getSheetAt(0); XSSFSheet sheet2 = wb.getSheetAt(1); // 在相应的单元格进行赋值 String header[] = { "姓名", "班级", "年龄", "住址", "联系人", "联系电话" }; XSSFRow row; XSSFCell cell[] = new XSSFCell[6]; for (int i = 1; i < 7; i++) { // 生成第i行 row = sheet1.createRow(i); for (int j = 0; j < 6; j++) { // 给这一行的第j列赋值 cell[j] = row.createCell(j); cell[j].setCellValue(header[j] + j); } } XSSFRow row1; XSSFCell cell1[] = new XSSFCell[3]; String header1[] = { "第一列", "第二列", "第三列" }; for (int i = 1; i < 21; i++) { row1 = sheet2.createRow(i); for (int j = 0; j < 3; j++) { cell1[j] = row1.createCell(j); cell1[j].setCellValue(header1[j] + j); } } FileOutputStream out = new FileOutputStream( "D:/temp/excelTest/export2.xlsx"); wb.write(out); out.close(); } }

浙公网安备 33010602011771号