我正在使用APACHE POI 3.13最新版本,将txt文件转换为excel文件并执行条件格式设置。但是,我没有获得3个色标(如热图),而是在输出excel文件的指定单元格中获得黑色。我无法进行3色缩放(颜色格式化)。我在互联网上查了一下,从那里找到了代码,但我不知道我在这里缺少什么。
代码
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.LinkedList;
import org.apache.poi.hssf.usermodel.HSSFColorScaleFormatting;
import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule;
import org.apache.poi.hssf.usermodel.HSSFFontFormatting;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFSheetConditionalFormatting;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ComparisonOperator;
import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
public class txtToExcel {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList<String[]> text_lines = new LinkedList<>();
try (BufferedReader br = new BufferedReader(new FileReader("C:\\Personal\\arpitData\\Data\\Brian\\Matrix_all_beh_v2.txt"))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
text_lines.add(sCurrentLine.split("\\t"));
}
} catch (IOException e) {
e.printStackTrace();
}
String fileName = "C:/Personal/arpitData/Data/Brian/Matrix_all_beh_v2_txt_to_excel.xls";
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Conditional_Formatting");
int row_num = 0;
for(String[] line : text_lines){
Row row = sheet.createRow(row_num++);
int cell_num = 0;
for(String value : line){
Cell cell = row.createCell(cell_num++);
if(cell_num == 1 || row_num ==1){
cell.setCellValue(value);
}
else{
cell.setCellValue(new BigDecimal(value).doubleValue());
}
}
}
/* Access conditional formatting facet layer */
HSSFSheetConditionalFormatting my_cond_format_layer = sheet.getSheetConditionalFormatting();
/* Create a Rule */
HSSFConditionalFormattingRule my_rule1 = my_cond_format_layer.createConditionalFormattingColorScaleRule();
/* Define color formatting if rule is met */
HSSFColorScaleFormatting my_rule_pattern1 = my_rule1.getColorScaleFormatting();
my_rule_pattern1.getThresholds()[0].setRangeType(RangeType.MIN);
my_rule_pattern1.getThresholds()[1].setRangeType(RangeType.PERCENTILE);
my_rule_pattern1.getThresholds()[1].setValue((double) 50);
my_rule_pattern1.getThresholds()[2].setRangeType(RangeType.PERCENTILE);
my_rule_pattern1.getThresholds()[2].setValue((double) 95);
my_rule_pattern1.getColors()[0].setARGBHex("FFFFFF");
my_rule_pattern1.getColors()[1].setARGBHex("F6FCFF");
my_rule_pattern1.getColors()[2].setARGBHex("FF6C6C");
/* Create a Cell Range Address */
CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("c2:I28")};
/* Attach rule to cell range */
my_cond_format_layer.addConditionalFormatting(my_data_range,my_rule1);
/* Write changes to the workbook */
try {
FileOutputStream out = new FileOutputStream(fileName);
workbook.write(out);
out.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
我的输入文件Matrix_all_beh_v2.txt就像
Total (2015/09/25) Obesity weight gain Body weight/body fat Adipocyte Endocrine PPAR RXR GR
"3,3,5,5-Tetrabromobisphenol A" 26 7 9 10 0 10 2 1
7-Diethylamino-4-methylcoumarin 1 0 0 0 0 1 0 0
"7,12-Dimethylbenz(a)anthracene" 140 84 20 16 10 16 6 9
"9-Aminoacridine, monohydrochloride, monohydrate" 0 0 0 0 0 0 0 0
"9,10-Dihydrobenzo[a]pyren-7(8H)-one" 0 0 0 0 0 0 0 0
4-Dodecylphenol 0 0 0 0 0 0 0 0
4-[3-(4-Acetyl-3-hydroxy-2-propylphenoxy)propoxy]phenoxy_acetic acid 47 4 7 11 2 32 9 0
答案 0 :(得分:0)
当我从HSSF切换到XSSF时,它确实有效。此外,还缺少一些jar库,我必须将其包括在内才能使其工作。