如何使用Apache POI设置Excel中的单元格值

huangapple go评论58阅读模式
英文:

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&lt;TaskEntity&gt; taskEntities) throws IOException {
String[] columns = {
&quot;taskId&quot;,
&quot;taskType&quot;,
&quot;taskName&quot;,
&quot;taskDescription&quot;,
&quot;taskPriority&quot;,
&quot;taskStatus&quot;,
&quot;loggedHours&quot;,
&quot;estimatedTimeInDays&quot;,
&quot;remainingTimeInDays&quot;,
&quot;taskLabel&quot;,
&quot;startDate&quot;,
&quot;endDate&quot;
};
try (Workbook workbook = new XSSFWorkbook();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Sheet sheet = workbook.createSheet(&quot;Tasks&quot;);
Row headerRow = sheet.createRow(0);
for (int col = 0; col &lt; 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 = &quot;D://excel/&quot; + &quot;task.xls&quot;;
FileOutputStream fileOutputStream = new FileOutputStream(filename);
fileOutputStream.write(outputStream.toByteArray());
return &quot;Excel exported successfully&quot;;
}
}
}

huangapple
  • 本文由 发表于 2023年6月5日 18:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定