其实这个初期版本已经基本上把他要的功能都实现了,呵呵
他的要求是批量转Excel,把所有用黄色标出的单元格所在的列拷到一个新文件中
其实底层很简单,界面做了倒有一会,还是一如既往的喜欢SWT啊~
附上Java利用Apache POI进行Excel文件的转换的核心代码。可能算法上不是很好,但数据量也不是很大。
  1
 public static HSSFWorkbook convert(HSSFWorkbook wb,byte[] rgb)
public static HSSFWorkbook convert(HSSFWorkbook wb,byte[] rgb) {
{    
2 short high = wb.getCustomPalette().findSimilarColor(rgb[0], rgb[1], rgb[2]).getIndex();
        short high = wb.getCustomPalette().findSimilarColor(rgb[0], rgb[1], rgb[2]).getIndex();
3 HSSFWorkbook dest = new HSSFWorkbook();
        HSSFWorkbook dest = new HSSFWorkbook();
4 FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
        FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
5 
        
6 Sheet sheet = wb.getSheetAt(0);
        Sheet sheet = wb.getSheetAt(0);
7 HashSet<Integer> toCopyColumns = new HashSet<Integer>();
        HashSet<Integer> toCopyColumns = new HashSet<Integer>();
8 
        
9 Cell copyHighCell = null;
        Cell copyHighCell = null;
10 Cell copyNormalCell = null;
        Cell copyNormalCell = null;
11 CellStyle csHigh;
        CellStyle csHigh;
12 CellStyle csNormal;
        CellStyle csNormal;
13 //read row
        //read row
14
 for (Row row:sheet)
        for (Row row:sheet) {
{
15
 for (Cell cell :row)
            for (Cell cell :row) {
{
16 //find high lighted cell
                //find high lighted cell
17
 if (cell.getCellStyle().getFillForegroundColor() == high)
                if (cell.getCellStyle().getFillForegroundColor() == high) {
{
18 //hit
                    //hit
19 toCopyColumns.add(cell.getColumnIndex());
                    toCopyColumns.add(cell.getColumnIndex());
20 copyHighCell = cell;
                    copyHighCell = cell;
21
 }else
                }else {
{
22 copyNormalCell = cell;
                    copyNormalCell = cell;
23 }
                }
24 }
            }
25 }
        }
26 csHigh = dest.createCellStyle();
        csHigh = dest.createCellStyle();
27 csNormal = dest.createCellStyle();
        csNormal = dest.createCellStyle();
28
 if (copyHighCell !=null)
        if (copyHighCell !=null) {
{
29 csHigh.cloneStyleFrom(copyHighCell.getCellStyle());
            csHigh.cloneStyleFrom(copyHighCell.getCellStyle());
30 copyHighCell = null;
            copyHighCell = null;
31 }
        }
32
 if (copyNormalCell !=null)
        if (copyNormalCell !=null) {
{
33 csNormal.cloneStyleFrom(copyNormalCell.getCellStyle());
            csNormal.cloneStyleFrom(copyNormalCell.getCellStyle());
34 copyNormalCell = null;
            copyNormalCell = null;
35 }
        }
36 
        
37 
        
38 
        
39 //read over, toCopyColumns now has all column that need to copy
        //read over, toCopyColumns now has all column that need to copy
40 Integer[] columns = new Integer[toCopyColumns.size()];
        Integer[] columns = new Integer[toCopyColumns.size()];
41 toCopyColumns.toArray(columns);
        toCopyColumns.toArray(columns);
42 Arrays.sort(columns);
        Arrays.sort(columns);
43 
        
44 Sheet destSheet = dest.createSheet("Sheet1");
        Sheet destSheet = dest.createSheet("Sheet1");
45
 for (Row row:sheet)
        for (Row row:sheet) {
{
46 Row newRow = destSheet.createRow(row.getRowNum());
            Row newRow = destSheet.createRow(row.getRowNum());
47
 for (int i = 0;i < columns.length;i++)
            for (int i = 0;i < columns.length;i++) {
{
48 Cell newCell = newRow.createCell(i);
                Cell newCell = newRow.createCell(i);
49 Cell oldCell = row.getCell(columns[i]);
                Cell oldCell = row.getCell(columns[i]);
50
 if (oldCell == null)
                if (oldCell == null) {
{
51 continue;
                    continue;
52 }
                }
53
 switch (oldCell.getCellType())
                switch (oldCell.getCellType()) {
{
54 case Cell.CELL_TYPE_FORMULA:
                    case Cell.CELL_TYPE_FORMULA:
55 CellValue value = fe.evaluate(oldCell);
                        CellValue value = fe.evaluate(oldCell);
56 //judge the new cell format
                        //judge the new cell format
57 newCell.setCellType(value.getCellType());
                        newCell.setCellType(value.getCellType());
58
 switch (value.getCellType())
                        switch (value.getCellType()) {
{
59 case Cell.CELL_TYPE_NUMERIC:
                            case Cell.CELL_TYPE_NUMERIC:
60 newCell.setCellValue(value.getNumberValue());break;
                                newCell.setCellValue(value.getNumberValue());break;
61 case Cell.CELL_TYPE_BOOLEAN:
                            case Cell.CELL_TYPE_BOOLEAN:
62 newCell.setCellValue(value.getBooleanValue());break;
                                newCell.setCellValue(value.getBooleanValue());break;
63 case Cell.CELL_TYPE_ERROR:
                            case Cell.CELL_TYPE_ERROR:
64 newCell.setCellErrorValue(value.getErrorValue());break;
                                newCell.setCellErrorValue(value.getErrorValue());break;
65 case Cell.CELL_TYPE_STRING:
                            case Cell.CELL_TYPE_STRING:
66 newCell.setCellValue(value.getStringValue());break;
                                newCell.setCellValue(value.getStringValue());break;
67 }
                        }
68 break;
                        break;
69 case Cell.CELL_TYPE_NUMERIC:
                    case Cell.CELL_TYPE_NUMERIC:
70 newCell.setCellValue(oldCell.getNumericCellValue());
                        newCell.setCellValue(oldCell.getNumericCellValue());
71 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;
72 case Cell.CELL_TYPE_BOOLEAN:
                    case Cell.CELL_TYPE_BOOLEAN:
73 newCell.setCellValue(oldCell.getBooleanCellValue());
                        newCell.setCellValue(oldCell.getBooleanCellValue());
74 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;
75 case Cell.CELL_TYPE_ERROR:
                    case Cell.CELL_TYPE_ERROR:
76 newCell.setCellErrorValue(oldCell.getErrorCellValue());
                        newCell.setCellErrorValue(oldCell.getErrorCellValue());
77 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;
78 case Cell.CELL_TYPE_STRING:
                    case Cell.CELL_TYPE_STRING:
79 newCell.setCellValue(oldCell.getStringCellValue());
                        newCell.setCellValue(oldCell.getStringCellValue());
80 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;
81 }
                }
82
 if (oldCell.getCellStyle().getFillForegroundColor() == high)
                if (oldCell.getCellStyle().getFillForegroundColor() == high) {
{
83 newCell.setCellStyle(csHigh);
                    newCell.setCellStyle(csHigh);
84
 }else
                }else {
{
85 newCell.setCellStyle(csNormal);
                    newCell.setCellStyle(csNormal);
86 }
                }
87 
                
88 }
            }
89 }
        }
90 //        System.out.println(wb.getNumCellStyles());
//        System.out.println(wb.getNumCellStyles());
91 //        System.out.println(dest.getNumCellStyles());
//        System.out.println(dest.getNumCellStyles());
92 return dest;
        return dest;
93 }
    }
94 
    
95
 public static void main(String args[]) throws FileNotFoundException, IOException
    public static void main(String args[]) throws FileNotFoundException, IOException {
{
96 
        
97 HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("test.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("test.xls"));
98
 HSSFWorkbook wb2 = convert (wb,new byte[]
        HSSFWorkbook wb2 = convert (wb,new byte[] {(byte) 0xff,(byte) 0xff,0x0});
{(byte) 0xff,(byte) 0xff,0x0});
99 wb2.write(new FileOutputStream("tt.xls"));
        wb2.write(new FileOutputStream("tt.xls"));
100 }
    }
 
 public static HSSFWorkbook convert(HSSFWorkbook wb,byte[] rgb)
public static HSSFWorkbook convert(HSSFWorkbook wb,byte[] rgb) {
{    2
 short high = wb.getCustomPalette().findSimilarColor(rgb[0], rgb[1], rgb[2]).getIndex();
        short high = wb.getCustomPalette().findSimilarColor(rgb[0], rgb[1], rgb[2]).getIndex();3
 HSSFWorkbook dest = new HSSFWorkbook();
        HSSFWorkbook dest = new HSSFWorkbook();4
 FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
        FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();5
 
        6
 Sheet sheet = wb.getSheetAt(0);
        Sheet sheet = wb.getSheetAt(0);7
 HashSet<Integer> toCopyColumns = new HashSet<Integer>();
        HashSet<Integer> toCopyColumns = new HashSet<Integer>();8
 
        9
 Cell copyHighCell = null;
        Cell copyHighCell = null;10
 Cell copyNormalCell = null;
        Cell copyNormalCell = null;11
 CellStyle csHigh;
        CellStyle csHigh;12
 CellStyle csNormal;
        CellStyle csNormal;13
 //read row
        //read row14

 for (Row row:sheet)
        for (Row row:sheet) {
{15

 for (Cell cell :row)
            for (Cell cell :row) {
{16
 //find high lighted cell
                //find high lighted cell17

 if (cell.getCellStyle().getFillForegroundColor() == high)
                if (cell.getCellStyle().getFillForegroundColor() == high) {
{18
 //hit
                    //hit19
 toCopyColumns.add(cell.getColumnIndex());
                    toCopyColumns.add(cell.getColumnIndex());20
 copyHighCell = cell;
                    copyHighCell = cell;21

 }else
                }else {
{22
 copyNormalCell = cell;
                    copyNormalCell = cell;23
 }
                }24
 }
            }25
 }
        }26
 csHigh = dest.createCellStyle();
        csHigh = dest.createCellStyle();27
 csNormal = dest.createCellStyle();
        csNormal = dest.createCellStyle();28

 if (copyHighCell !=null)
        if (copyHighCell !=null) {
{29
 csHigh.cloneStyleFrom(copyHighCell.getCellStyle());
            csHigh.cloneStyleFrom(copyHighCell.getCellStyle());30
 copyHighCell = null;
            copyHighCell = null;31
 }
        }32

 if (copyNormalCell !=null)
        if (copyNormalCell !=null) {
{33
 csNormal.cloneStyleFrom(copyNormalCell.getCellStyle());
            csNormal.cloneStyleFrom(copyNormalCell.getCellStyle());34
 copyNormalCell = null;
            copyNormalCell = null;35
 }
        }36
 
        37
 
        38
 
        39
 //read over, toCopyColumns now has all column that need to copy
        //read over, toCopyColumns now has all column that need to copy40
 Integer[] columns = new Integer[toCopyColumns.size()];
        Integer[] columns = new Integer[toCopyColumns.size()];41
 toCopyColumns.toArray(columns);
        toCopyColumns.toArray(columns);42
 Arrays.sort(columns);
        Arrays.sort(columns);43
 
        44
 Sheet destSheet = dest.createSheet("Sheet1");
        Sheet destSheet = dest.createSheet("Sheet1");45

 for (Row row:sheet)
        for (Row row:sheet) {
{46
 Row newRow = destSheet.createRow(row.getRowNum());
            Row newRow = destSheet.createRow(row.getRowNum());47

 for (int i = 0;i < columns.length;i++)
            for (int i = 0;i < columns.length;i++) {
{48
 Cell newCell = newRow.createCell(i);
                Cell newCell = newRow.createCell(i);49
 Cell oldCell = row.getCell(columns[i]);
                Cell oldCell = row.getCell(columns[i]);50

 if (oldCell == null)
                if (oldCell == null) {
{51
 continue;
                    continue;52
 }
                }53

 switch (oldCell.getCellType())
                switch (oldCell.getCellType()) {
{54
 case Cell.CELL_TYPE_FORMULA:
                    case Cell.CELL_TYPE_FORMULA:55
 CellValue value = fe.evaluate(oldCell);
                        CellValue value = fe.evaluate(oldCell);56
 //judge the new cell format
                        //judge the new cell format57
 newCell.setCellType(value.getCellType());
                        newCell.setCellType(value.getCellType());58

 switch (value.getCellType())
                        switch (value.getCellType()) {
{59
 case Cell.CELL_TYPE_NUMERIC:
                            case Cell.CELL_TYPE_NUMERIC:60
 newCell.setCellValue(value.getNumberValue());break;
                                newCell.setCellValue(value.getNumberValue());break;61
 case Cell.CELL_TYPE_BOOLEAN:
                            case Cell.CELL_TYPE_BOOLEAN:62
 newCell.setCellValue(value.getBooleanValue());break;
                                newCell.setCellValue(value.getBooleanValue());break;63
 case Cell.CELL_TYPE_ERROR:
                            case Cell.CELL_TYPE_ERROR:64
 newCell.setCellErrorValue(value.getErrorValue());break;
                                newCell.setCellErrorValue(value.getErrorValue());break;65
 case Cell.CELL_TYPE_STRING:
                            case Cell.CELL_TYPE_STRING:66
 newCell.setCellValue(value.getStringValue());break;
                                newCell.setCellValue(value.getStringValue());break;67
 }
                        }68
 break;
                        break;69
 case Cell.CELL_TYPE_NUMERIC:
                    case Cell.CELL_TYPE_NUMERIC:70
 newCell.setCellValue(oldCell.getNumericCellValue());
                        newCell.setCellValue(oldCell.getNumericCellValue());71
 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;72
 case Cell.CELL_TYPE_BOOLEAN:
                    case Cell.CELL_TYPE_BOOLEAN:73
 newCell.setCellValue(oldCell.getBooleanCellValue());
                        newCell.setCellValue(oldCell.getBooleanCellValue());74
 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;75
 case Cell.CELL_TYPE_ERROR:
                    case Cell.CELL_TYPE_ERROR:76
 newCell.setCellErrorValue(oldCell.getErrorCellValue());
                        newCell.setCellErrorValue(oldCell.getErrorCellValue());77
 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;78
 case Cell.CELL_TYPE_STRING:
                    case Cell.CELL_TYPE_STRING:79
 newCell.setCellValue(oldCell.getStringCellValue());
                        newCell.setCellValue(oldCell.getStringCellValue());80
 newCell.setCellType(oldCell.getCellType());break;
                        newCell.setCellType(oldCell.getCellType());break;81
 }
                }82

 if (oldCell.getCellStyle().getFillForegroundColor() == high)
                if (oldCell.getCellStyle().getFillForegroundColor() == high) {
{83
 newCell.setCellStyle(csHigh);
                    newCell.setCellStyle(csHigh);84

 }else
                }else {
{85
 newCell.setCellStyle(csNormal);
                    newCell.setCellStyle(csNormal);86
 }
                }87
 
                88
 }
            }89
 }
        }90
 //        System.out.println(wb.getNumCellStyles());
//        System.out.println(wb.getNumCellStyles());91
 //        System.out.println(dest.getNumCellStyles());
//        System.out.println(dest.getNumCellStyles());92
 return dest;
        return dest;93
 }
    }94
 
    95

 public static void main(String args[]) throws FileNotFoundException, IOException
    public static void main(String args[]) throws FileNotFoundException, IOException {
{96
 
        97
 HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("test.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("test.xls"));98

 HSSFWorkbook wb2 = convert (wb,new byte[]
        HSSFWorkbook wb2 = convert (wb,new byte[] {(byte) 0xff,(byte) 0xff,0x0});
{(byte) 0xff,(byte) 0xff,0x0});99
 wb2.write(new FileOutputStream("tt.xls"));
        wb2.write(new FileOutputStream("tt.xls"));100
 }
    }
其中需要注意一点:对于一个Workbook而言,同样的单元格样式会存储在同一个样式属性中,由不同的单元格分别引用
所以一个个复制单元格的属性是不行滴,这样会造成目标文件样式数过大。
也没有什么好办法,因为源文件简单,所以就流氓了一下,哪位有更好的办法能压缩这个样式表,让重复的样式合并呢?
上个图
