zoukankan      html  css  js  c++  java
  • 根据指定的excel模板导出数据

    //模板

     //java代码

    package molde;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import org.apache.poi.ss.usermodel.Cell;
    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;
    
    public class ExportExcelUtil {
    	public static void main(String[] args) throws Exception {
    	
    		ExportExcelUtil export = new ExportExcelUtil();
    		String srcFilePath = "C:\Users\Pei\Desktop\86.xlsx";
    		String fileName = "test_" + System.currentTimeMillis() + ".xlsx";
    		String desFilePath = "d:/" + fileName;
    		
    		export.exportExcel(srcFilePath,desFilePath);
    	}
    	
    	//根据指定的excel模板导出数据
    	public void exportExcel(String srcFilePath,String desFilePath) throws Exception {
    		//创建Excel文件的输入流对象
    		FileInputStream fis = new FileInputStream(srcFilePath);
    		//根据模板创建excel工作簿
    		XSSFWorkbook workBook = new XSSFWorkbook(fis);
    		//创建Excel文件输出流对象
    		FileOutputStream fos = new FileOutputStream(desFilePath);
    		//获取创建的工作簿第一页
    		XSSFSheet sheet = workBook.getSheetAt(0);
    		//给指定的sheet命名
    		workBook.setSheetName(0,"2016-11-30");
    		
    		//修改标题
    		XSSFRow row = sheet.getRow(0);
    		XSSFCell cell = row.getCell(0);
    		//获取指定单元格值
    		String s = cell.getStringCellValue();
    		cell.setCellValue("修改后的标题为:"+s);
    		
    		//获取当前sheet最后一行数据对应的行索引
    		int currentLastRowIndex = sheet.getLastRowNum();
    		int newRowIndex = currentLastRowIndex + 1;
    		XSSFRow newRow = sheet.createRow(newRowIndex);
    		//开始创建并设置该行每一单元格的信息,该行单元格的索引从 0 开始
    		int cellIndex = 0;
    		//创建一个单元格,设置其内的数据格式为字符串,并填充内容,其余单元格类同
    		XSSFCell newNameCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
    		newNameCell.setCellValue("乔玉宝");
    		XSSFCell newGenderCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
    		newGenderCell.setCellValue("男");
    		XSSFCell newAgeCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
    		newAgeCell.setCellValue(25);
    		XSSFCell newAddressCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
    		newAddressCell.setCellValue("重庆市渝北区");
    		workBook.write(fos);
    		//关闭流
    		fis.close();
    		fos.flush();
    		fos.close();
    		System.out.println("导出成功");
    	}
    }
    

      //pom.xml文件

    <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.17</version>
            </dependency>
            <dependency>
              <groupId>net.sf.jxls</groupId>
              <artifactId>jxls-core</artifactId>
              <version>1.0.3</version>
          </dependency>
    	  <dependency> 
    		<groupId>javax.servlet</groupId> 
    		<artifactId>javax.servlet-api</artifactId> 
    		<version>3.1.0</version> 
    	</dependency>
    	<dependency>
    	    <groupId>org.springframework</groupId>
    	    <artifactId>spring-web</artifactId>
    	    <version>4.1.6.RELEASE</version>
    	</dependency>
    

      

  • 相关阅读:
    地理大发现
    克里斯托弗·哥伦布
    2016. last day in office
    泰斯花粉阻隔剂 怎么使用
    ZT 螨虫知识2
    ZT 螨虫的话就不要跟狗多接触,狗的寄生虫很多,还有草地,
    expense KK [ɪkˋspɛns] DJ [iksˋpens]
    Windows 实战项目 001 文件扫描器 (01)
    017 系统内存信息 内存大小 空闲内存 5
    017 虚拟内存页面区块 4
  • 原文地址:https://www.cnblogs.com/xianz666/p/14500696.html
Copyright © 2011-2022 走看看