Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 用java编写的excel文件更改顺序_Mysql_Excel - Fatal编程技术网

Mysql 用java编写的excel文件更改顺序

Mysql 用java编写的excel文件更改顺序,mysql,excel,Mysql,Excel,我将数据库中的数据存储到ArrayList中,然后从ArrayList中获取数据并写入excel文件,供用户下载。当我写入excel文件时,它会写得非常完美。但是当我写入更多数据时,最后一行名为total的数据会出现在其他行中。编码如下 public String excel_missedcall(List<datefilter> result1){ Date date = new Date(System.currentTimeMillis());

我将数据库中的数据存储到ArrayList中,然后从ArrayList中获取数据并写入excel文件,供用户下载。当我写入excel文件时,它会写得非常完美。但是当我写入更多数据时,最后一行名为total的数据会出现在其他行中。编码如下

public String excel_missedcall(List<datefilter> result1){
            Date date = new Date(System.currentTimeMillis());
        String download_file="";
        Properties prop = new Properties();
        InputStream input = null;
    String link="";
    String linkfilepath="";
//read file name from properties file
        try {
            String filename = "config.properties";
            input = MainController.class.getClassLoader().getResourceAsStream(
                    filename);
            if (input == null) {
                System.out.println("Sorry, unable to find " + filename);

            }
            // load a properties file from class path, inside static method
            prop.load(input);
            String filepath=prop.getProperty("missedcall_filePath");
            download_file=filepath+"missedcall_"+date.getTime()+".xlsx";
            link=prop.getProperty("missedcall_downloadfilePath");
            linkfilepath=link+"missedcall_"+date.getTime()+".xlsx";
            int total=0;
        //create excel sheet
                    //Blank workbook
                    XSSFWorkbook workbook = new XSSFWorkbook();

                    //Create a blank sheet
                    XSSFSheet sheet= workbook.createSheet("Missedcall");

                    //This data needs to be written (Object[])
                    Map<String, Object[]> data = new TreeMap<String, Object[]>();
                    data.put("1", new Object[] {"Date", "TotalCalls"});

                     for(int i=0;i<result1.size();i++){
                        data.put(""+(i+2),new Object[] {result1.get(i).getMisscall_date(),Integer.parseInt(result1.get(i).getSum_misscall())});
                        total+=Integer.parseInt( result1.get(i).getSum_misscall());
                        //System.out.println("**********1*********"+(i+2));
                    } 

                  data.put(""+(result1.size()+2), new Object[] {"Total", total});
                    //Iterate over data and write to sheet
                    Set<String> keyset = data.keySet();
                    int rownum = 0;
                    for (String key : keyset)
                    {
                        System.out.println("*********1**********"+key);

                        Row row = sheet.createRow(rownum++);
                        System.out.println("*********2**********"+rownum);

                        Object [] objArr = data.get(key);
                        int cellnum = 0;
                        for (Object obj : objArr)
                        {
                           Cell cell = row.createCell(cellnum++);
                           if(obj instanceof String)
                                cell.setCellValue((String)obj);
                            else if(obj instanceof Integer)
                                cell.setCellValue((Integer)obj);
                        }
                    }
                    try
                    {
                        //Write the workbook in file system
                        FileOutputStream out1 = new FileOutputStream(new File(download_file));
                        workbook.write(out1);
                        out1.close();
                        System.out.println("xlsx written successfully on disk.");
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return linkfilepath;
        }

请帮助我。

您正在使用字符串作为键,请替换以下行

Map<String, Object[]> data = new TreeMap<String, Object[]>();

Map<String, Object[]> data = new TreeMap<String, Object[]>();
Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();