public class ExcelUtil {
Workbook tempWorkBook = null;
Sheet tempSheet = null;
Row tempRow = null;
Cell tempCell = null;
InputStream inputstream = null;
File file= null;
OutputStream outputstream = null;
//自动添加行起始点
int num = 15;
public void setStream(InputStream inputstream,OutputStream outputstream){
this.inputstream = inputstream;
this.outputstream = outputstream;
}
public void setUrl(String intString,String outString){
File file= null;
try {
file = new File(outString);
if(!file.exists()){
file.createNewFile();
}
this.outputstream = new FileOutputStream(file);
this.inputstream = new FileInputStream(intString);
} catch (Exception e) {
// TODO: handle exception
}
}
public void createExcel() {
try {
// 获取模板
tempWorkBook = new XSSFWorkbook(inputstream);
// 获取模板sheet页
tempSheet = tempWorkBook.getSheetAt(0);
// 将数据写入excel
// 获取指定行
tempRow = tempSheet.createRow(1);
tempCell = tempRow.createCell(2) ;
tempCell.setCellValue("大公司就是牛");
tempRow = tempSheet.getRow(3);
tempCell = tempRow.createCell(1) ;
tempCell.setCellValue("中国");
int len = 6;
for(int i=0;i
// 创建一行 统计
tempRow = tempSheet.createRow(num+i);
// 获取单元格
tempCell = tempRow.createCell(0);
tempCell.setCellValue("i="+i);
tempCell = tempRow.createCell(1);
tempCell.setCellValue("i="+i);
tempCell = tempRow.createCell(2);
tempCell.setCellValue("i="+i);
tempCell = tempRow.createCell(3);
tempCell.setCellValue("i="+i);
}
CellRangeAddress cra=new CellRangeAddress(num+len+1,num+len+1,0,1);
//在sheet里增加合并单元格
tempSheet.addMergedRegion(cra);
tempRow = tempSheet.createRow(num+len+1);
tempCell = tempRow.createCell(0);
tempCell.setCellValue("Subtotal 小计");
tempCell = tempRow.createCell(2);
tempCell.setCellValue("2");
tempCell = tempRow.createCell(5);
tempCell.setCellValue("2");
// 将内容写入Excel
tempWorkBook.write(outputstream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class test {
public static void main(String[] s) {
ExcelUtil excelUtil = new ExcelUtil();
File file= null;
OutputStream outputstream = null;
InputStream inputstream = null;
try {
file = new File("f:\\out0420.xlsx");
if(!file.exists()){
file.createNewFile();
}
outputstream = new FileOutputStream(file);
inputstream = new FileInputStream("f:\\model0420.xlsx");
} catch (Exception e) {
// TODO: handle exception
}
excelUtil.setStream(inputstream, outputstream);
excelUtil.createExcel();
}
}