用Java写一个将list集合写入excel,并且关键字标红的demo
时间: 2024-05-09 09:21:32 浏览: 84
好的,以下是Java写一个将list集合写入excel,并且关键字标红的demo:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWriterDemo {
public static void writeListToExcel(List<String> list, String filePath, String keyword) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Font font = workbook.createFont();
font.setColor(IndexedColors.RED.getIndex());
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
int rownum = 0;
for (String str : list) {
Row row = sheet.createRow(rownum++);
Cell cell = row.createCell(0);
if (str.contains(keyword)) {
cell.setCellValue(str);
cell.setCellStyle(cellStyle);
} else {
cell.setCellValue(str);
}
}
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
}
```
请问,还有什么其他问题吗?
阅读全文