我用谷歌搜索了一些代码,找到了一些答案,但无法以粗体显示我的excel文件输出并设置背景颜色。我尝试了以下代码。你能告诉我我哪里出错了吗?请看看。谢谢。
FYI:我将用蓝色或任何浅色背景制作粗体第一行。如果你知道,请帮助代码。
// Excel file generation code
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Readings");
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short)11);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(HSSFFont.COLOR_NORMAL);
font.setBold(true);
font.setColor(HSSFColor.DARK_BLUE.index);
style.setFont(font);
// Freeze 1st Row
sheet.createFreezePane(0, 1);
HSSFRow row = sheet.createRow(1);
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.setRowStyle(style);
rowhead.createCell(0).setCellValue("RUN");
rowhead.createCell(1).setCellValue("NUMBER");
您在以下方面做错了:
1-您没有设置任何背景颜色;
2-当您创建新单元格时,它们会覆盖行样式,因此您需要为您创建的每个新单元格设置样式;
以下是工作代码:
FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Readings");
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short)11);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(HSSFFont.COLOR_NORMAL);
font.setBold(true);
font.setColor(HSSFColor.DARK_BLUE.index);
style.setFont(font);
//Add these lines
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
sheet.createFreezePane(0, 1); // Freeze 1st Row sheet.createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow)
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.setRowStyle(style);
//Set the cell0 Style
HSSFCell cell0 = rowhead.createCell(0);
cell0.setCellStyle(style);
cell0.setCellValue("ROW");
//Set the cell1 Style
HSSFCell cell1 = rowhead.createCell(1);
cell1.setCellStyle(style);
cell1.setCellValue("NUMBER");
workbook.write(fileOut);
文件输出:
首先删除font. setBold权重(HSSFFont.COLOR_NORMAL);
。COLOR_NORMAL不是真正的Bold权重,而是一种颜色。font.setBold(true);
应该足以加粗您的文本。
您还需要在样式中添加一些属性以获得背景颜色。这是令人困惑的部分。方法是使用填充。填充的前景和背景颜色与单元格的其余部分不同。要获得实心填充,您需要使用:
style.setForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
请注意,这将填充的前景色设置为浅蓝色。由于填充具有实心前景,这就是您所需要的。如果您选择了其中一种模式填充,则还需要设置填充的背景色。填充本身就是单元格背景。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class excelStylingExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
String toCreateFullPath="C:\\Users\\Your Path\\testExcel.xlsx";
Path path=Paths.get(toCreateFullPath);
if(!Files.exists(path.getParent())){
try {
Files.createDirectory(path.getParent());
System.out.println("Directory created");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error Directory is Not Created");
}
}
XSSFWorkbook workbook =new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("testSheet");
String[] columns = {"Code", "Name", "Address" };
CellStyle hStyle=null;
// Creating a font
XSSFFont font= workbook.createFont();
font.setFontHeightInPoints((short)12);
font.setFontName("Arial");
font.setColor(IndexedColors.YELLOW.getIndex());
font.setBold(true);
font.setItalic(false);
hStyle=workbook.createCellStyle();
hStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
hStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
hStyle.setAlignment(CellStyle.ALIGN_CENTER);
// Setting font to style
hStyle.setFont(font);
// Create a Row
Row headerRow = sheet.createRow(0);
// Create cells
for(int i = 0; i < columns.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columns[i]);
// Setting cell style
cell.setCellStyle(hStyle);
sheet.autoSizeColumn(i);
}
// setting values into records
int rowCount = 0;
for(int i=0;i<1;i++) { // an example of one row
CellStyle styleAllCell=null;
styleAllCell=workbook.createCellStyle();
styleAllCell.setAlignment(CellStyle.ALIGN_CENTER);
Row row = sheet.createRow(++rowCount);
Cell cell = row.createCell(0);
cell.setCellValue("1000");
sheet.autoSizeColumn(0);
cell.setCellStyle(styleAllCell);
cell = row.createCell(1);
cell.setCellValue("Leaonardo");
sheet.autoSizeColumn(1);
cell.setCellStyle(styleAllCell);
cell = row.createCell(2);
cell.setCellValue("Italy");
sheet.autoSizeColumn(2);
cell.setCellStyle(styleAllCell);
}
try (FileOutputStream outputStream = new FileOutputStream(toCreateFullPath)) {
workbook.write(outputStream);
}
}
}