英文:
How to set cell value in excel using apache poi
问题
我正在尝试像这样设置单元格的值:
Cell cell = getCell(sheet, cellAddress);
cell.setCellValue("test3");
这个方法有效。问题是,工作簿中已经定义了背景颜色,我不想失去它。
我尝试过:
Cell cell = getCell(sheet, cellAddress);
CellStyle style = cell.getCellStyle();
cell.setCellValue("test3");
cell.setCellStyle(style);
但这会移除绿色背景,只保留设置的文本。
请帮忙,
Michal
英文:
i am trying to setCell value like here:
Cell cell = getCell(sheet, cellAddress);
cell.setCellValue("test3");
and this is working.
The issue is that i have already background color defined in workbook and do not want to loose it.
I tried to:
Cell cell = getCell(sheet, cellAddress);
CellStyle style = cell.getCellStyle();
cell.setCellValue("test3");
cell.setCellStyle(style);
But this is removing green background and keeping only set up text.
Please help,
Michal
答案1
得分: 4
以下是您的代码的中文翻译:
public class ExcelGenerator {
public static String taskToExcel(List<TaskEntity> taskEntities) throws IOException {
String[] columns = {
"任务ID",
"任务类型",
"任务名称",
"任务描述",
"任务优先级",
"任务状态",
"已记录工时",
"估计完成时间(天)",
"剩余时间(天)",
"任务标签",
"开始日期",
"结束日期"
};
try (Workbook workbook = new XSSFWorkbook();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Sheet sheet = workbook.createSheet("任务列表");
Row headerRow = sheet.createRow(0);
for (int col = 0; col < columns.length; col++) {
Cell cell = headerRow.createCell(col);
cell.setCellValue(columns[col]);
}
int rowIndex = 1;
for (TaskEntity task : taskEntities) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(task.getTaskId());
row.createCell(1).setCellValue(task.getTaskType());
row.createCell(2).setCellValue(task.getTaskName());
row.createCell(3).setCellValue(task.getTaskDescription());
row.createCell(4).setCellValue(task.getTaskPriority());
row.createCell(5).setCellValue(task.getTaskStatus());
row.createCell(6).setCellValue(task.getLoggedHours());
row.createCell(7).setCellValue(task.getEstimatedTimeInDays());
row.createCell(8).setCellValue(task.getRemainingTimeInDays());
row.createCell(9).setCellValue(task.getTaskLabel());
row.createCell(10).setCellValue(String.valueOf(task.getStartDate()));
row.createCell(11).setCellValue(String.valueOf(task.getEndDate()));
}
workbook.write(outputStream);
String filename = "D://excel/" + "任务列表.xls";
FileOutputStream fileOutputStream = new FileOutputStream(filename);
fileOutputStream.write(outputStream.toByteArray());
return "成功导出Excel";
}
}
}
请注意,我已将代码中的英文列名翻译成了中文。
英文:
You can take reference from my code :
public class ExcelGenerator {
public static String taskToExcel(List<TaskEntity> taskEntities) throws IOException {
String[] columns = {
"taskId",
"taskType",
"taskName",
"taskDescription",
"taskPriority",
"taskStatus",
"loggedHours",
"estimatedTimeInDays",
"remainingTimeInDays",
"taskLabel",
"startDate",
"endDate"
};
try (Workbook workbook = new XSSFWorkbook();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Sheet sheet = workbook.createSheet("Tasks");
Row headerRow = sheet.createRow(0);
for (int col = 0; col < columns.length; col++) {
Cell cell = headerRow.createCell(col);
cell.setCellValue(columns[col]);
}
int rowIndex = 1;
for (TaskEntity task : taskEntities) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(task.getTaskId());
row.createCell(1).setCellValue(task.getTaskType());
row.createCell(2).setCellValue(task.getTaskName());
row.createCell(3).setCellValue(task.getTaskDescription());
row.createCell(4).setCellValue(task.getTaskPriority());
row.createCell(5).setCellValue(task.getTaskStatus());
row.createCell(6).setCellValue(task.getLoggedHours());
row.createCell(7).setCellValue(task.getEstimatedTimeInDays());
row.createCell(8).setCellValue(task.getRemainingTimeInDays());
row.createCell(9).setCellValue(task.getTaskLabel());
row.createCell(10).setCellValue(String.valueOf(task.getStartDate()));
row.createCell(11).setCellValue(String.valueOf(task.getEndDate()));
}
workbook.write(outputStream);
String filename = "D://excel/" + "task.xls";
FileOutputStream fileOutputStream = new FileOutputStream(filename);
fileOutputStream.write(outputStream.toByteArray());
return "Excel exported successfully";
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论