简体   繁体   中英

Sort data before exporting to excel

I have the code below, and what this does is basically get my data from my table model and put this into a spreadsheet. However upon exporting the data the data is not being kept in the order i specified when sorting my table:

This is defined as below from another method that sorts the table rows and the executes the method saveSingleTableAsExcel(); which exports the data:

.......
    sorter = new TableRowSorter<>(tableR.getModel());
        tableR.setRowSorter(sorter);
        sortKeys = new ArrayList<>();

        int columnIndexToSort = 0;
        sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.ASCENDING));

        sorter.setSortKeys(sortKeys);
        sorter.sort(); 
                saveSingleTableAsExcel();

.......

public void saveSingleTableAsExcel() throws FileNotFoundException{

        Map<String,TableModel> models = new HashMap<String,TableModel>();


        models.put("Sheet1", modelR);

        saveTablesAsExcel(models);
    }



    public static void saveTablesAsExcel(Map<String,TableModel> models) throws FileNotFoundException{

        HSSFWorkbook wb = new HSSFWorkbook(); 

        for (String sheetName : models.keySet()){
            createSheet(wb, models.get(sheetName), sheetName);
        }
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HHmmss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22
        //FileOutputStream out = null;

               FileOutputStream out = new FileOutputStream("C:\\Users\\tester.xls");

        try {
            wb.write(out);
            out.close();
        } catch (IOException e) {
        }
    }
        /**
     * Create a Sheet in the workbook using data from the TableModel
     * 
     * @param wb
     * @param model
     * @param sheetName
     */   
       private static void createSheet(HSSFWorkbook wb, TableModel model, String sheetName){
        Sheet sheet = wb.createSheet(sheetName);    

        Row headerRow = sheet.createRow(0);
        headerRow.setHeightInPoints(12.75f);

        HSSFFont boldFont = wb.createFont();
        boldFont.setFontHeightInPoints((short)22);
        boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        HSSFCellStyle headerStyle = wb.createCellStyle();

        // Create the header cells
        int numColumns = model.getColumnCount();
        for (int col=0; col<numColumns; col++) {    
            Cell cell = headerRow.createCell(col);
            cell.setCellValue(model.getColumnName(col));
            cell.setCellStyle(headerStyle);
        }

        // Set the cell values
        int numRows = model.getRowCount();
        for (int row=0; row<numRows; row++){    
            Row sheetRow = sheet.createRow(row+1);  // account for header row (0)

            for (int col=0; col<numColumns; col++) {
                Cell cell = sheetRow.createCell(col);
                Object val = model.getValueAt(row, col);

                if (val instanceof Number){
                    cell.setCellValue((double)val); 
                }
                else if (val instanceof Boolean){
                    cell.setCellValue((Boolean)val);    
                }

                else if (val instanceof String){
                    cell.setCellValue(((String)val));   
                }
                else if (val instanceof Date){
                    cell.setCellValue((Date)val);
                }
                            //    else {
                //  cell.setCellValue(val.toString());
                               // }
            }
        }
    }

How can i retain the same order as the model, which is being sorted by the first column (column 0)?

Is your sort method using a comparator or equals method that haven't been overwritten? That's my best guess without seeing the sort method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM