0
我將名爲sCurrentLine的字符串轉換爲Excel的行。根據該行是以X,Y或Z開始,我爲該行着色。HSSFCell setCellStyle在所有工作表的單元格上應用樣式,而不是選定的單元格
row = sheet.createRow(lines);
String[] parts = sCurrentLine.split("\\|");
if (sCurrentLine.contains("X")) {
bgColorIndex = HSSFColor.RED.index;
} else if (sCurrentLine.contains("Y")){
bgColorIndex = HSSFColor.LIGHT_BLUE.index;
} else if (sCurrentLine.contains("Z")) {
bgColorIndex = HSSFColor.YELLOW.index;
} else {
bgColorIndex = HSSFColor.BROWN.index;
}
所以我有這個變量稱爲bgColorIndex並用它來設置行的所有單元格的顏色
for (short i = 0; i < parts.length; i++) {
row.createCell(i).setCellValue(parts[i]);
HSSFCell curCell = row.getCell(i);
HSSFCellStyle curStyle = curCell.getCellStyle();
curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
curStyle.setFillForegroundColor(bgColorIndex);
curStyle.setFillBackgroundColor(bgColorIndex);
System.out.println("Color is: " + bgColorIndex);
curCell.setCellStyle(curStyle);
}
lines++;
}
我遇到的問題是setCellStyle是應用樣式的所有行和圖紙,所以我檢測到的最後一種顏色應用於整個文檔。
我該如何獨立着色每個細胞?
編輯: 整個代碼:
private void generateCSVFile() {
String filename = "excel.xls" ;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow((short)0);
// header
rowhead.createCell((short)0).setCellValue("AXIS");
rowhead.createCell((short)1).setCellValue("INIT");
rowhead.createCell((short)2).setCellValue("MID");
rowhead.createCell((short)3).setCellValue("END");
// Set columns width
for (short i = 0; i < 3; i++) {
sheet.setColumnWidth(i, (short)(20*300));
}
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader("data.txt");
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader("data.txt"));
short lines = 1;
HSSFRow row = null;
while ((sCurrentLine = br.readLine()) != null) {
if (sCurrentLine.contains("AXIS")) {
lines = 0;
sheet = workbook.createSheet("SecondSheet");
// Set columns width
for (short i = 0; i < 7; i++) {
sheet.setColumnWidth(i, (short)(20*256));
}
row = sheet.createRow(lines);
}
row = sheet.createRow(lines);
String[] parts = sCurrentLine.split("\\|");
short bgColorIndex = 0;
// Check the first cell to set color for X, Y or Z
if (sCurrentLine.contains("X")) {
bgColorIndex = HSSFColor.RED.index;
} else if (sCurrentLine.contains("Y")){
bgColorIndex = HSSFColor.LIGHT_BLUE.index;
} else if (sCurrentLine.contains("Z")) {
bgColorIndex = HSSFColor.YELLOW.index;
} else {
bgColorIndex = HSSFColor.BROWN.index;
}
for (short i = 0; i < parts.length; i++) {
row.createCell(i).setCellValue(parts[i]);
HSSFCell curCell = row.getCell(i);
HSSFCellStyle curStyle = curCell.getCellStyle();
curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
curStyle.setFillForegroundColor(bgColorIndex);
curStyle.setFillBackgroundColor(bgColorIndex);
System.out.println("Color is: " + bgColorIndex);
curCell.setCellStyle(curStyle);
curStyle = null;
}
lines++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
你能提供整個代碼塊?看起來你首先找到了顏色,因此記住了最後一個,然後將其設置到單元格。 – NiVeR