java读取和生成表格

public class excel_r_or_w {

    public static void main(String[] args) {
        exportexcel();
    }

    //从excel获取值
    public static Map<String, Map<String, Object>> getexcel() {
        String path = "C:/Users/Desktop/xxxxx.xlsx";//导入excel文件路径及文件名
        //summap用于保存从excel获取的所有数据
        Map<String, Map<String, Object>> summap = new LinkedHashMap<String, Map<String, Object>>();
        //map用于保存当前行的数据
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            //读取 xls/xlsx 创建excel工作簿
            FileInputStream excelFileInputStream = new FileInputStream(path);
            XSSFWorkbook workbook = new XSSFWorkbook(excelFileInputStream);
            XSSFSheet sheet = workbook.getSheetAt(workbook.getNumberOfSheets()-1);

            //用于记录当前行的id
            int index = 0;

            for (int rowIndex = 4; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                // XSSFRow 代表当前行数据
                XSSFRow row = sheet.getRow(rowIndex);
                if (row == null) {
                continue;
                }
                //设置列数据类型
                XSSFCell Cell1 = row.getCell(0);
                Cell1.setCellType(Cell.CELL_TYPE_STRING);
                XSSFCell Cell2 = row.getCell(1);
                Cell2.setCellType(Cell.CELL_TYPE_NUMERIC);
                XSSFCell Cell3 = row.getCell(2);
                Cell3.setCellType(Cell.CELL_TYPE_NUMERIC);

                //把当前行数据保存到map
                map.put("Cell1", Cell1.getStringCellValue());
                map.put("Cell2", Cell2.getNumericCellValue());
                map.put("Cell3", Cell3.getNumericCellValue());

                //保存map到summap中 以行id为key (copy()用来防止当前行数据被下一行的数据覆盖)
                summap.put(String.valueOf(index), copy(map));
                index = index + 1;

                }
            //用完要close workbook和excelFileInputStream
            workbook.close();
            excelFileInputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return summap ;
    }

    //导出数据到excel
    public static void exportexcel(){

        String filepath = "C:/Users/Otto/Desktop/excel/";
        long currenttime = System.currentTimeMillis();
        String filename = String.valueOf(currenttime);
        File file=new File(filepath);
        if (!file.exists()) {
            file.mkdir();
        }
        File file1=new File(filepath + filename + "/");//导出excel的文件路径 如:C:/Users/Otto/Desktop/excel/时间/xxxxx.xls
        if(!file1.exists()){
            file1.mkdir();
        }
        filename = filepath + filename + "/";

        int indexrow = 0;
        int count = 0;
        //声明一个工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        //声明表
        HSSFSheet sheet = wb.createSheet("sheet");
        //表格设置
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        //创建表格头
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell1"); 
            cell.setCellStyle(style);
        cell = row.createCell(1);  
            cell.setCellValue("Cell2");  
            cell.setCellStyle(style);  
        cell = row.createCell(2);  
            cell.setCellValue("Cell3");  
            cell.setCellStyle(style); 

        try {
            //设置导出地址
            FileOutputStream out = new FileOutputStream(filename + String.valueOf(currenttime) + ".xlsx");
            //插入数据到excel
            for (Map.Entry<String, Map<String, Object>> smap : getexcel().entrySet()) {   //读取getexcel里面获取到的数据 可以换成数据库查询结果

                row = sheet.createRow(indexrow + 1);
                row.createCell(0).setCellValue((String) smap.getValue().get("Cell1"));
                row.createCell(1).setCellValue((Double) smap.getValue().get("Cell2"));
                row.createCell(2).setCellValue((Double) smap.getValue().get("Cell3"));
                //row.createCell(3).setCellValue("这里放数据");
                indexrow = indexrow + 1 ;
                count = count + 1;
            }       
            wb.write(out);
            out.close();
            wb.close();
            System.out.println("导出成功!");
        } catch (FileNotFoundException e) {
            System.out.println("找不到路径 导出失败!");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件流出错 导出失败!");
            e.printStackTrace();
        }

    }

    //防止数据被覆盖
    public static Map<String, Object> copy(Map<String, Object> map){

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("Cell1", map.get("Cell1"));
        resultMap.put("Cell2", map.get("Cell2"));
        resultMap.put("Cell3", map.get("Cell3"));

        return resultMap;
    }
}

用的是poi的jar包 getexcel()方法读取 exportexcel()是生成 exportexcel()中的for循环可替换成其他 如:数据库读取的while循环

附件:http://download.csdn.net/detail/qq_22778717/9632684

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值