如何使用java为Excel工作表定义动态单元格范围? [英] How to define dynamic cell range for Excel sheet using java?
本文介绍了如何使用java为Excel工作表定义动态单元格范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule( "MOD(ROW() - 1, 1) = 0");
PatternFormatting fill = rule.createPatternFormatting();
style.setFillForegroundColor(IndexedColors.BLUE.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
FontFormatting ffRed = rule.createFontFormatting();
ffRed.setFontColorIndex(IndexedColors.WHITE.index);
CellRangeAddress[] regions = { CellRangeAddress.valueOf("A1:ZZ1") };
sheetCF.addConditionalFormatting(regions,rule);
ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
"MOD(ROW() - 1, 2) = 0");
PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.PALE_BLUE.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);
CellRangeAddress[] regions1 = { CellRangeAddress.valueOf("A2:ZZ8304") };
sheetCF.addConditionalFormatting(regions1,rule1);
在 CellRangeAddress.values 中,我给出了静态值.但我希望它是动态的.也就是说,只要有数据存在,它就应该动态地获取范围.如何解决这个问题?我提到了定义动态单元格范围,但没有奏效.
Inside CellRangeAddress.values I gave static value. But I wanted it to be dynamic. That is wherever there's data present it should take range dynamically. How to resolve this issue? I referred to define dynamic cell range but it didn't work.
推荐答案
你可以这样试试
String name = CellReference.convertNumToColString(cell.getColumnIndex());
String location = "$" + name + "$" + cell.getRowIndex() + ":$" + name + "$" + (cell.getRowIndex() + 1);
CellRangeAddress[] regions = { CellRangeAddress.valueOf(location) };
这篇关于如何使用java为Excel工作表定义动态单元格范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文