我需要使用Apache创建一个渐变填充单元格,如下图所示。
我使用了这段代码来创建一个简单的渐变填充,但我需要与图片完全一样。
谢谢你们的帮助。
PS:图片显示,我所说的渐变填充单元格。

                XSSFWorkbook workbook = new XSSFWorkbook();
                Sheet sheet = workbook.createSheet();
                Row row = sheet.createRow(0);
                XSSFCellStyle cellstyle = workbook.createCellStyle();
                //set pattern fill settings only to have some fill to get the fill index from it
                cellstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                //get fill index used in this CellStyle
                int fillidx = (int)cellstyle.getCoreXf().getFillId();
                //get the low level CTFill used in this CellStyle
                CTFill ctfill = workbook.getStylesSource().getFillAt(fillidx).getCTFill();
                System.out.println(ctfill);
                //unset the pattern fill
                ctfill.unsetPatternFill();
                //now low level set the gradient fill
                byte[] rgb1 = new byte[3];
                rgb1[0] = (byte) 0; // red
                rgb1[1] = (byte) 102; // green
                rgb1[2] = (byte) 102; // blue
                byte[] rgb2 = new byte[3];
                rgb2[0] = (byte) 255; // red
                rgb2[1] = (byte) 255; // green
                rgb2[2] = (byte) 255; // blue
                CTGradientFill ctgradientfill = ctfill.addNewGradientFill();
                ctgradientfill.setDegree(45.0);
                ctgradientfill.addNewStop().setPosition(0.0);
                ctgradientfill.getStopArray(0).addNewColor().setRgb(rgb1);
                ctgradientfill.addNewStop().setPosition(0.9);
                ctgradientfill.getStopArray(1).addNewColor().setRgb(rgb2);
                ctgradientfill.addNewStop().setPosition(1.0);
                ctgradientfill.getStopArray(2).addNewColor().setRgb(rgb1);
                System.out.println(ctfill);
                Cell cell = row.createCell(0);
                cell.setCellValue("");
                cell.setCellStyle(cellstyle);
                workbook.write(new FileOutputStream("C:/file.xlsx"));
                workbook.close();谢谢你帮忙。
发布于 2017-11-07 09:05:41
Sub gradient()
'
' gradient Macro
'
'
    With Selection.Interior
        .Pattern = xlPatternRectangularGradient
        .gradient.RectangleLeft = 1
        .gradient.RectangleRight = 1
        .gradient.RectangleTop = 0
        .gradient.RectangleBottom = 0
        .gradient.ColorStops.Clear
    End With
    With Selection.Interior.gradient.ColorStops.Add(0)
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.Interior.gradient.ColorStops.Add(1)
        .ThemeColor = xlThemeColorAccent5
        .TintAndShade = -0.498031556138798
    End With
End Sub发布于 2017-11-06 18:30:03
尝试使用记录宏获取所需的内容,然后播放并查看它是如何工作的,但如果失败,则可以使用此引用VBA代码单元格填充(颜色、模式和梯度)手动编辑。
https://stackoverflow.com/questions/47140195
复制相似问题