我有一个单元格,有些文本是斜体,有些文本不是斜体。如何保留格式?我查看了 OfficeOpenXml.Style.ExcelStyle 并看到了粗体、斜体等选项,但这些选项适用于整个单元格。有没有办法使用 eppplus 判断哪些文本以某种方式格式化并保留该格式?
FileInfo info = new FileInfo(@"C:\excel.xlsx");
using (ExcelPackage package = new ExcelPackage(info)) {
ExcelWorksheet sheet = package.Workbook.Worksheets["Sheet"];
object text = sheet.Cells[1, 1].Value;
// if I do toString, I lose all formatting:
string textWithOutFormat = text.ToString();
// I would assume I'd get it some how like this?
if (sheet.Cells[1, 1].IsRichText) {
// get rich text formatting or get what items are wrapp in rich text
// no idea how to get it.
}
}
请您参考如下方法:
编写具有多种样式的单元格:
FileInfo fi = new FileInfo(@"c:\File.xlsx");
using (ExcelPackage package = new ExcelPackage(fi))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheets"];
worksheet.Cells[1, 1].IsRichText = true;
ExcelRichText richtext = worksheet.Cells[1, 1].RichText.Add("Test Italic");
richtext .Italic = true;
richtext = worksheet.Cells[1, 1].RichText.Add("Test 2");
richtext .Italic = false;
package.Save();
}
读取具有多种样式的单元格:
foreach(var part in cell.RichText)
{
if (part.Bold)
// Do staff
if (part.Italic)
// Do staff
}
