栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java excel 线性表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java excel 线性表

package com.chenkang.mybatisplus;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;



public class ExcelUtils {
    

    
    public static void copyExcel(String modelExcel, String newExcel, String newName) {
        HSSFWorkbook wb = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {

            File file = new File(newExcel);
            if (!file.exists()) {
                file.mkdirs();
            }
            fis = new FileInputStream(modelExcel);
            fos = new FileOutputStream(newExcel + "/" + newName);
            wb = new HSSFWorkbook(fis);
            wb.write(fos);
        } catch (Exception e) {
            System.err.println("复制excel表格异常1:" + e.getMessage());
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {

                System.err.println("复制excel表格异常2:" + e.getMessage());
            }
        }
    }

}
package com.chenkang.mybatisplus;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.FileInputStream;
import java.io.FileOutputStream;


public class A {

    public static void main(String[] args) {
        try {

            //模板文件的绝对路径
            String modelExcel = "E:\aa.xls";
            String fileName="aa.xls";

            //复制模板
            ExcelUtils.copyExcel(modelExcel, "E:\img\",fileName);
            //新文件的路径
            fileName="E:\img\"+fileName;
            
            FileInputStream fs=new FileInputStream(fileName);  //获取d://test.xls  
            POIFSFileSystem ps=new POIFSFileSystem(fs);  //使用POI提供的方法得到excel的信息  
            HSSFWorkbook wb=new HSSFWorkbook(ps);
            HSSFSheet sheet=wb.getSheetAt(0);  //获取到工作表,因为一个excel可能有多个工作表  
            

            FileOutputStream out=new FileOutputStream(fileName);  //向d://test.xls中写数据

            HSSFRow row = sheet.createRow(23);
            row.createCell(1).setCellValue("10:00");
            row.createCell(2).setCellValue("10:00");
            row.createCell(3).setCellValue("10:00");
            row.createCell(4).setCellValue("10:00");
            

            HSSFRow row2 = sheet.createRow(24);
            row2.createCell(0).setCellValue("温度");
            row2.createCell(1).setCellValue(new Double("25"));
            row2.createCell(2).setCellValue(new Double("29"));
            row2.createCell(3).setCellValue(new Double("35"));
            row2.createCell(4).setCellValue(new Double("18"));



            HSSFRow row3 = sheet.createRow(25);
            row3.createCell(0).setCellValue("湿度");
            row3.createCell(1).setCellValue("10");
            row3.createCell(2).setCellValue("20");
            row3.createCell(3).setCellValue("20");
            row3.createCell(4).setCellValue("30");

            out.flush();
            wb.write(out);
            out.close();
        } catch (Exception e) {

        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918963.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号