EPPlus Excel 行高不一致

2024-05-16

我已经使用 EPPlus 生成了一个 excel 文件,在 MS Office 2007 中一切似乎都很完美,但客户端使用的是 MS Office 2010/2013,并且在第 29 行之后未设置行高。

这是一个非常奇怪的问题,我已经尝试了 4 天,但无法解决它。

我附上了 Excel 的问题截图。

List<spGetInventoryPrintForLabel> inventories = coreInventory.GetInventoryListForLabel(inventoryIDs);
                string fileName = Path.Combine(Server.MapPath("~/ExcelLabelDocuments"), "Skyltar " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx");

                ExcelPackage pack = new ExcelPackage();
                ExcelWorksheet ws = pack.Workbook.Worksheets.Add("Blad1");
                ws.PrinterSettings.PaperSize = ePaperSize.A4;
                ws.Cells["A:XFD"].Style.Font.Name = "Arial";
                ws.Cells["A:XFD"].Style.Font.Size = 10;
                ws.DefaultRowHeight = 16.5;
                ws.View.ShowGridLines = false;

                ws.Column(1).Width = ws.Column(3).Width = ws.Column(4).Width = ws.Column(6).Width = 4;
                ws.Column(2).Width = ws.Column(5).Width = 34.5;

                int sizeRowOfLabel = 10;
                int recordCount = 0;
                int labelStartIndex, rowIndex;
                labelStartIndex = rowIndex = 2;
                string column = "B";               

                using (System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Images/PrintLogo.jpg")))
                {
                    foreach (spGetInventoryPrintForLabel rw in inventories)
                    {
                        if (recordCount % 2 == 0)
                        {
                            column = "B";
                            ws.Cells["A" + labelStartIndex + ":C" + labelStartIndex].Style.Border.Top.Style = ExcelBorderStyle.Dashed;
                            ws.Cells["C" + labelStartIndex + ":C" + (sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Right.Style = ExcelBorderStyle.Dashed;
                            ws.Cells["A" + (sizeRowOfLabel + labelStartIndex - 1) + ":C" + (sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
                            rowIndex = labelStartIndex;
                            if ((sizeRowOfLabel + labelStartIndex - 1) % 41 == 0)
                            {
                                ws.Row(sizeRowOfLabel + labelStartIndex - 1).PageBreak = true;
                            }
                        }
                        else
                        {
                            column = "E";
                            ws.Cells["D" + labelStartIndex + ":F" + labelStartIndex].Style.Border.Top.Style = ExcelBorderStyle.Dashed;
                            ws.Cells["F" + labelStartIndex + ":F" + (sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Right.Style = ExcelBorderStyle.Dashed;
                            ws.Cells["D" + (sizeRowOfLabel + labelStartIndex - 1) + ":F" + (sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Bottom.Style = ExcelBorderStyle.Dashed;
                            rowIndex = labelStartIndex;
                            labelStartIndex += sizeRowOfLabel;
                        }

                        //blank
                        rowIndex++;
                        ws.Cells[column + rowIndex].Style.Font.Bold = true;
                        //Artist
                        ws.Cells[column + rowIndex].Value = rw.ArtistName.Trim() == "," ? "" : rw.ArtistName; rowIndex++;
                        //Artist year (f. BirthYear) OR (BirthYear - DeathYear)
                        string artistBirth = string.Empty;
                        if (string.IsNullOrEmpty(rw.ArtistDeath))
                            artistBirth = (string.IsNullOrEmpty(rw.ArtistBirth) ? "" : "f. " + rw.ArtistBirth);
                        else
                            artistBirth = rw.ArtistBirth + " - " + rw.ArtistDeath;
                        ws.Cells[column + rowIndex].Value = artistBirth; rowIndex++;
                        //blank
                        rowIndex++;
                        //Art title
                        ws.Cells[column + rowIndex + ":" + column + (rowIndex + 1)].Merge = true;
                        ws.Cells[column + rowIndex].Style.Font.Bold = true;
                        ws.Cells[column + rowIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
                        ws.Cells[column + rowIndex].Style.WrapText = true;
                        ws.Cells[column + rowIndex].Value = rw.ArtTitle + (string.IsNullOrEmpty(rw.PurchaseYear) ? "" : ", " + rw.PurchaseYear); rowIndex++;
                        //blank
                        rowIndex++;
                        //Category
                        string category = string.Empty;
                        if (!string.IsNullOrEmpty(rw.ArtTechnologyCategory))
                        {
                            category = rw.ArtTechnologyCategory;
                            category += (string.IsNullOrEmpty(rw.ArtSubCategory1)) ? "" : ", " + rw.ArtSubCategory1;
                            category += (string.IsNullOrEmpty(rw.ArtSubCategory2)) ? "" : ", " + rw.ArtSubCategory2;
                        }
                        else
                        {
                            category = (string.IsNullOrEmpty(rw.ArtSubCategory1)) ? "" : rw.ArtSubCategory1;
                            category += (string.IsNullOrEmpty(rw.ArtSubCategory2)) ? "" : ", " + rw.ArtSubCategory2;
                        }
                        ws.Cells[column + rowIndex].Value = category; rowIndex++;
                        //SK Number
                        ws.Cells[column + rowIndex + ":" + column + (rowIndex + 1)].Merge = true;
                        ws.Cells[column + rowIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
                        ws.Cells[column + rowIndex].Value = rw.SKNumber; //rowIndex++;
                        //Logo
                        //ws.Cells[column + rowIndex + ":" + column + (rowIndex + 1)].Merge = true;
                        ExcelPicture pic = ws.Drawings.AddPicture(rw.SKNumber, img);
                        pic.From.Column = column == "B" ? 1 : 4;
                        pic.From.Row = rowIndex - 1;
                        pic.From.ColumnOff = 160 * 9525;
                        pic.From.RowOff = 4 * 9525;
                        pic.SetSize(img.Width, img.Height);

                        recordCount++;
                    }
                }

如果你这样做怎么办:

for (var i = 1; i <= 110; i++)
{
    ws.Row(i).Height = 20;
}

我将您的代码放入快速单元测试中,它似乎成功了:

[TestMethod]
public void RowHeight_Test()
{
    var inventories = new List<RowObject>();

    for (var i = 0; i < 20; i++)
    {
        inventories.Add(new RowObject
        {
            ArtistName = "DOB - " + i,
            ArtSubCategory1 = "Cat 1 - " + i,
            ArtSubCategory2 = "Cat 2 - " + i,
            ArtTechnologyCategory = "Tech Cat - " + i,
            ArtTitle = "Title - " + i,
            ArtistBirth = "DOB - " + i,
            ArtistDeath = "DOD - " + i,
            PurchaseYear = "Year Purchased - " + i,
            SKNumber = "SKU Number - " + i,
        });
    }

    string fileName = "c:/temp/temp.xlsx";
    var newFile = new FileInfo(fileName);
    if (newFile.Exists)
        newFile.Delete();

    using (ExcelPackage pack = new ExcelPackage(newFile))
    {
        ExcelWorksheet ws = pack.Workbook.Worksheets.Add("Blad1");
        ws.PrinterSettings.PaperSize = ePaperSize.A4;
        ws.Cells["A:XFD"].Style.Font.Name = "Arial";
        ws.Cells["A:XFD"].Style.Font.Size = 10;
        ws.DefaultRowHeight = 16.5;
        ws.View.ShowGridLines = false;

        ws.Column(1).Width = ws.Column(3).Width = ws.Column(4).Width = ws.Column(6).Width = 4;
        ws.Column(2).Width = ws.Column(5).Width = 34.5;

        int sizeRowOfLabel = 10;
        int recordCount = 0;
        int labelStartIndex, rowIndex;
        labelStartIndex = rowIndex = 2;
        string column = "B";

        using (System.Drawing.Image img = System.Drawing.Image.FromFile("c:/temp/logo.png"))
        {
            foreach (RowObject rw in inventories)
            {
                if (recordCount%2 == 0)
                {
                    column = "B";
                    ws.Cells["A" + labelStartIndex + ":C" + labelStartIndex].Style.Border.Top.Style =ExcelBorderStyle.Dashed;
                    ws.Cells["C" + labelStartIndex + ":C" + (sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Right.Style = ExcelBorderStyle.Dashed;
                    ws.Cells[
                        "A" + (sizeRowOfLabel + labelStartIndex - 1) + ":C" +
                        (sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Bottom.Style =
                        ExcelBorderStyle.Dashed;
                    rowIndex = labelStartIndex;
                    if ((sizeRowOfLabel + labelStartIndex - 1)%41 == 0)
                    {
                        ws.Row(sizeRowOfLabel + labelStartIndex - 1).PageBreak = true;
                    }
                }
                else
                {
                    column = "E";
                    ws.Cells["D" + labelStartIndex + ":F" + labelStartIndex].Style.Border.Top.Style =ExcelBorderStyle.Dashed;
                    ws.Cells["F" + labelStartIndex + ":F" + (sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Right.Style = ExcelBorderStyle.Dashed;
                    ws.Cells["D" + (sizeRowOfLabel + labelStartIndex - 1) + ":F" +(sizeRowOfLabel + labelStartIndex - 1)].Style.Border.Bottom.Style =ExcelBorderStyle.Dashed;
                    rowIndex = labelStartIndex;
                    labelStartIndex += sizeRowOfLabel;
                }

                //blank
                rowIndex++;
                ws.Cells[column + rowIndex].Style.Font.Bold = true;
                //Artist
                ws.Cells[column + rowIndex].Value = rw.ArtistName.Trim() == "," ? "" : rw.ArtistName;
                rowIndex++;
                //Artist year (f. BirthYear) OR (BirthYear - DeathYear)
                string artistBirth = string.Empty;
                if (string.IsNullOrEmpty(rw.ArtistDeath))
                    artistBirth = (string.IsNullOrEmpty(rw.ArtistBirth) ? "" : "f. " + rw.ArtistBirth);
                else
                    artistBirth = rw.ArtistBirth + " - " + rw.ArtistDeath;
                ws.Cells[column + rowIndex].Value = artistBirth;
                rowIndex++;
                //blank
                rowIndex++;
                //Art title
                ws.Cells[column + rowIndex + ":" + column + (rowIndex + 1)].Merge = true;
                ws.Cells[column + rowIndex].Style.Font.Bold = true;
                ws.Cells[column + rowIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
                ws.Cells[column + rowIndex].Style.WrapText = true;
                ws.Cells[column + rowIndex].Value = rw.ArtTitle +
                                                    (string.IsNullOrEmpty(rw.PurchaseYear)
                                                        ? ""
                                                        : ", " + rw.PurchaseYear);
                rowIndex++;
                //blank
                rowIndex++;
                //Category
                string category = string.Empty;
                if (!string.IsNullOrEmpty(rw.ArtTechnologyCategory))
                {
                    category = rw.ArtTechnologyCategory;
                    category += (string.IsNullOrEmpty(rw.ArtSubCategory1)) ? "" : ", " + rw.ArtSubCategory1;
                    category += (string.IsNullOrEmpty(rw.ArtSubCategory2)) ? "" : ", " + rw.ArtSubCategory2;
                }
                else
                {
                    category = (string.IsNullOrEmpty(rw.ArtSubCategory1)) ? "" : rw.ArtSubCategory1;
                    category += (string.IsNullOrEmpty(rw.ArtSubCategory2)) ? "" : ", " + rw.ArtSubCategory2;
                }
                ws.Cells[column + rowIndex].Value = category;
                rowIndex++;
                //SK Number
                ws.Cells[column + rowIndex + ":" + column + (rowIndex + 1)].Merge = true;
                ws.Cells[column + rowIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
                ws.Cells[column + rowIndex].Value = rw.SKNumber; //rowIndex++;
                //Logo
                //ws.Cells[column + rowIndex + ":" + column + (rowIndex + 1)].Merge = true;
                ExcelPicture pic = ws.Drawings.AddPicture(rw.SKNumber, img);
                pic.From.Column = column == "B" ? 1 : 4;
                pic.From.Row = rowIndex - 1;
                pic.From.ColumnOff = 160*9525;
                pic.From.RowOff = 4*9525;
                pic.SetSize(img.Width, img.Height);

                recordCount++;
            }
        }

        for (var i = 1; i <= 110; i++)
        {
            ws.Row(i).Height = 20;
        }

        pack.Save();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EPPlus Excel 行高不一致 的相关文章

  • Environment.CurrentDirectory 与 System.IO.Directory.GetCurrentDirectory

    我正在编写一个 Net WinForms 并不断在调试和发布配置之间切换 并且有一些文件我需要任一配置才能访问 我想做的是将文件放在 BIN 文件夹中的公共目录中 这样它看起来像这样 MyProject Bin CommonFiles My
  • 未找到 Boost 库,但编译正常

    我正在尝试在 C 中使用 boost 的文件系统 使用时看起来编译没问题 c c Analyse c o Analyse o g W Wall L usr local lib lboost filesystem lboost system
  • 循环遍历 C 结构中的元素以提取单个元素的值和数据类型

    我有一个要求 我有一个 C 语言的大结构 由大约 30 多个不同数据类型的不同元素组成 typedef struct type1 element1 type2 element2 type3 element3 type2 element4 1
  • 从复选框列表中选择循环生成的复选框中的一个复选框

    抱歉我的英语不好 在我的 ASP NET 网站上 我从 SQL 表导入软件列表 看起来像这样 但实际上要长得多 Microsoft Application Error Reporting br br Microsoft Applicatio
  • 传递 constexpr 对象

    我决定给予新的C 14的定义constexpr旋转并充分利用它 我决定编写一个小的编译时字符串解析器 然而 我正在努力保持我的对象constexpr将其传递给函数时 考虑以下代码 include
  • 如何将 .txt 文件中的数据转换为 xml? C#

    我在一个文本文件中有数千行数据 我想通过将其转换为更容易搜索的内容来轻松搜索 我希望 XML 或其他类型的大型数据结构 尽管我不确定它是否是最好的对于我的想法 每行的数据如下所示 第 31 册 托马斯 乔治 32 34 154 每本书都不是
  • 语音识别编程问题入门

    所以 你们可能都看过 钢铁侠 其中托尼与一个名为贾维斯的人工智能系统进行交互 演示剪辑here http www youtube com watch v Go8zsh1Ev6Y 抱歉 这是广告 我非常熟悉 C C 和 Visual Basi
  • Eigen 和 OpenMP:由于错误共享和线程开销而没有并行化

    系统规格 Intel Xeon E7 v3 处理器 4 插槽 16 核 插槽 2 线程 核心 Eigen 系列和 C 的使用 以下是代码片段的串行实现 Eigen VectorXd get Row const int j const int
  • 获取没有显式特征的整数模板参数的有符号/无符号变体

    我希望定义一个模板类 其模板参数始终是整数类型 该类将包含两个成员 其中之一是类型T 另一个作为类型的无符号变体T 即如果T int then T Unsigned unsigned int 我的第一直觉是这样做 template
  • 是否使用 C# 数据集? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我对 C 中的数据集概念有点困惑 编码 ASP NET 站点 但这并不重要 在我的阅读中 我了解到它们 本质上 用作我的应用程序和我的
  • 不可变类与结构

    以下是类与 C 中的结构的唯一区别 如果我错了 请纠正我 类变量是引用 而结构变量是值 因此在赋值和参数传递中复制结构的整个值 类变量是存储在堆栈上的指针 指向堆上的内存 而结构变量作为值存储在堆上 假设我有一个不可变的结构 该结构的字段一
  • 将 Word 转换为 PDF - 禁用“保存”对话框

    我有一个用 C 编写的 Word 到 PDF 转换器 除了一件事之外 它工作得很好 有时 在某些 Word 文件上 后台会出现一条消息保存源文件中的更改 gt 是 否 取消 但我没有对源文件进行任何更改 我只想从 Word 文件创建 PDF
  • 将函数参数类型提取为参数包

    这是一个后续问题 解包 元组以调用匹配的函数指针 https stackoverflow com questions 7858817 unpacking a tuple to call a matching function pointer
  • 模板类中的无效数据类型生成编译时错误?

    我正在使用 C 创建一个字符串类 我希望该类仅接受数据类型 char 和 wchar t 并且我希望编译器在编译时使用 error 捕获任何无效数据类型 我不喜欢使用assert 我怎样才能做到这一点 您可以使用静态断言 促进提供一个 ht
  • Visual Studio 2015 - Web 项目上缺少共享项目参考选项卡

    我从 MSDN 订阅升级到 Visual Studio 2015 因为我非常兴奋地阅读有关共享项目的信息 当我们想要做的只是重用代码时 不再需要在依赖项中管理 21382 个 nuget 包 所以我构建了一个测试共享项目 其中包含一些代码
  • 导入到 SQL Server 时忽略 Excel 文件中的列

    我有多个具有相同格式的 Excel 文件 我需要将它们导入 SQL Server 我当前遇到的问题是 有两个文本列我需要完全忽略 因为它们是自由文本 并且某些行的字符长度超出了服务器允许我导入的长度 这会导致截断错误 因为我的分析不需要这些
  • 如何在 sql azure 上运行 aspnet_regsql? [复制]

    这个问题在这里已经有答案了 可能的重复 将 ASP NET 成员资格数据库迁移到 SQL Azure https stackoverflow com questions 10140774 migrating asp net membersh
  • 是否允许全局静态标识符以单个 _ 开头?

    换句话说 可能static 文件范围 全局变量恰好以一个下划线开头 而不会产生与 C 实现发生名称冲突的可能性 https www gnu org software libc manual html node Reserved Names
  • MySqlConnectionStringBuilder - 使用证书连接

    我正在尝试连接到 Google Cloud Sql 这是一个 MySql 解决方案 我能够使用 MySql Workbench 进行连接 我如何使用 C 连接MySqlConnectionStringBuilder 我找不到提供这三个证书的
  • 当用户更改 Windows 中的语言键盘布局时如何通知?

    I want to show a message to user when the user changes the language keyboard layout of Windows for example from EN to FR

随机推荐

  • DbGeography 多边形到 JSON

    我将 DbGeography 多边形存储在数据库中 我的控制器从数据库获取多边形 我需要将它们转换为 JSON var polygons db Areas Where x gt x Type type Select x gt new Vie
  • JSON 中的哈希到底是什么?

    我正在学习 JSON 但我发现你也可以将所谓的 哈希 放入 JSON 中 我在哪里可以找到什么是哈希 或者你能向我解释一下什么是哈希吗 另外 什么是哈希图 我有 C 和 C 经验 正在学习 JS Jquery 和 JSON 哈希是一个稀疏数
  • 与 SQL 中的 IN 运算符相反

    我怎么能做相反的事情 换句话说 选择所有姓氏不是 Hansen 或 Pettersen 的人 WHERE lastname NOT IN Hansen Pettersen 请参阅 IN 和 NOT IN 运算符 部分SQLite 所理解的
  • linq2sql,存储库模式 - 如何从两个或多个表查询数据?

    我使用存储库模式 和 linq2sql 作为数据访问 并拥有例如 ProductsRep 和 CustomersRep 在非常简单的场景中 数据库有两个表 产品 产品 ID 客户 ID 产品名称 日期 和顾客 客户 ID 名字 姓氏 每个存
  • 实体框架中的批量插入

    我使用批量插入插入大量记录 例如 20K 当我仅插入一个实体时 它会正常工作 但是 当我用来插入多个实体 例如一对多 时 它将仅插入父实体 而不会插入子实体 我的实体和代码 Customer cs public class Customer
  • 使用 pandas.date_range() 生成多个日期时间,每周两个日期

    我在用着pd date range start date end date freq W MON 每周一生成每周频率日期时间start date 2017 01 01 and end date 2017 12 31 这意味着每月大约生成 4
  • Tkinter 菜单删除项

    如何删除任何菜单项 例如我想删除 播放 self menubar Menu self root self root config menu self menubar self filemenu2 Menu self menubar self
  • ValueError:请使用“Layer”实例初始化“TimeDistributed”层

    我正在尝试构建一个可以在音频和视频样本上进行训练的模型 但出现此错误ValueError Please initialize TimeDistributed layer with a Layer instance You passed Te
  • Laravel Passport,通过密码客户端进行多个连接

    我无法理解如何使用 Laravel Passport 通过密码客户端为同一用户实现多个连接 我有一个移动应用程序 需要与基于 Laravel 的 API 进行通信 我的用户在首次启动应用程序时必须输入他们的login and passwor
  • 两个日期范围之间相交的天数

    有人知道如何最好地计算两个日期范围之间相交的天数吗 这是我写的一个小方法来计算这个 private static int inclusiveDays DateTime s1 DateTime e1 DateTime s2 DateTime
  • 将 MouseListener 添加到面板

    我正在尝试将鼠标操作添加到我的面板中 这就是程序应该做的事情 编写一个程序 允许用户通过按三下鼠标来指定一个三角形 第一次按下鼠标后 画一个小点 第二次按下鼠标后 绘制一条连接前两个点的线 第三次按下鼠标后 绘制整个三角形 第四次按下鼠标会
  • 仅删除多对多关系

    我有一个 has and belongs to many friends join table gt friends peoples 要添加朋友 我会这样做 people followers lt lt friend这会创建关系和新的个人资
  • CQRS/ES 世界中的报告

    我想我理解 ES CQRS 背景下的读取模型的想法 如果不明白请纠正我 然而 我对于在 严肃 报道的背景下使用它仍然有一些疑问 假设我使用关系数据库加上一些 ORM 来整理我的读取模型 一个基本的 摘要统计读取模型 可能如下所示 class
  • 外部类与单例类

    假设我们使用 extern 关键字有一些外部链接 我有 在class1 cpp中 MyClass myClassVar NULL 构造函数初始化上述内容 析构函数删除 然后在class2 cpp和class3 cpp中有 extern My
  • 使用 cfdocument 时仅将页脚添加到最后一页

    我正在使用创建多页文档cfdocument 使用动态文本创建 因此可以有任意数量的页面 甚至一页 我可以用
  • 如何为 Nslocal 通知设置自定义重复间隔......?

    我是 iphone 开发新手 我正在尝试在我的项目中使用 NslocalNotification 我需要每 2 小时或每两天或每两个月等给出提醒 目前我正在使用 NslocalNotification 重复间隔 但它仅适用于使用 Ncale
  • 从空缓冲区构造“std::ostream”是否有效?

    考虑以下 std ostream out nullptr 这是合法且明确定义的吗 如果我现在这样做怎么样 out lt lt hello world n 这是合法且明确定义的吗 如果是这样 大概这是一种无操作 是的 实例化该流是合法且定义明
  • 将 yerr/xerr 绘制为阴影区域而不是误差线

    在 matplotlib 中 如何将误差绘制为阴影区域而不是误差条 例如 而不是 忽略示例图中各点之间的平滑插值 这需要进行一些手动插值 或者只是获得更高分辨率的数据 您可以使用pyplot fill between https matpl
  • 如何将彼此“接近”的纬度/经度点分组?

    我有一个用户提交的纬度 经度点的数据库 并且正在尝试将 接近 点分组在一起 接近 是相对的 但目前看来约为 500 英尺 起初 我似乎只能按前 3 个小数位具有相同纬度 经度的行进行分组 大约是一个 300x300 的盒子 了解当您远离赤道
  • EPPlus Excel 行高不一致

    我已经使用 EPPlus 生成了一个 excel 文件 在 MS Office 2007 中一切似乎都很完美 但客户端使用的是 MS Office 2010 2013 并且在第 29 行之后未设置行高 这是一个非常奇怪的问题 我已经尝试了