【问题标题】:Display cell with vertical text using EPPlus使用 EPPlus 显示带有垂直文本的单元格
【发布时间】:2019-08-22 06:39:30
【问题描述】:

使用 EPPlus,如何更改单元格文本以显示为这样的垂直文本,

在excel中,你可以在设置单元格方向的时候点击这个按钮,

我正在尝试使用.TextRotation,但这并没有达到我想要的效果,将其设置为 180 度会给我这样的结果,

ws.Cells[row, 2].Style.TextRotation = 180;, .TextRotation 只接受整数值,所以我想知道如何获取“文本”按钮的值,

【问题讨论】:

  • 我对此很感兴趣,并像您在 Excel 中想要的那样堆叠文本,然后使用 EPPlus 在 C# 中打开该文件。带有堆叠文本的单元格将 VerticalAlignment 设置为底部,TextRotation 值设置为 255。当我尝试以编程方式将 TextRotation 值设置为 255 时,我得到了 `ArgumentOutOfRangeException`(可以在 source 第 181 行中看到. 所以,尝试联系 EPPlus 开发者。:)
  • @Nino 谢谢,我会这样做的。另外,我从没想过手动创建 excel 文件并使用 EPPlus 读取它,所以谢谢你,这将使我在尝试创建具有大量格式的 Excel 时更轻松。
  • 很高兴能帮到你;如果没有答案,那么至少有评论:)

标签: c# .net excel epplus


【解决方案1】:

这绝对是您发现的错误。有一种方法,但它很丑陋。当您将其更改为默认值以外的任何内容时,您可以使用单元格创建的StyleID:

[TestMethod]
public void Text_Rotate_Test()
{
    //https://stackoverflow.com/questions/57603348/display-cell-with-vertical-text-using-epplus

    var fileInfo = new FileInfo(@"c:\temp\Text_Rotate_Test.xlsx");
    if (fileInfo.Exists)
        fileInfo.Delete();

    using (var pck = new ExcelPackage(fileInfo))
    {
        var workbook = pck.Workbook;
        var worksheet = workbook.Worksheets.Add("Sheet1");
        var cell = worksheet.Cells[1, 1];

        cell.Value = "Test Text Value";

        //Trigger epplus to create a new style specific for the cell.
        //This needs to be done even thought it will be overridden in 
        //order to ref by index.  But have to be careful not to step
        //on other styles so make it as unique as it needs to be.
        cell.Style.TextRotation = 180;

        //Make sure the update the xml before looking up by index
        workbook.Styles.UpdateXml();
        workbook.Styles.CellXfs[cell.StyleID].TextRotation = 255;

        pck.Save();
    }
}

这给出了这个:

【讨论】:

  • 干得好。我认为这可能可行,但我肯定会想念workbook.Styles.UpdateXml(); 之类的东西。
  • 完美,谢谢。我试图用更新的代码重建一个 dll,但没有成功,这无疑为我节省了很多尝试重建项目的时间。
猜你喜欢
  • 2012-11-05
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 2011-05-29
  • 2016-03-15
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
相关资源
最近更新 更多