使用EPPlus将Excel工作表格式化为表格 [英] Format a Excel Sheet as a Table using EPPlus

查看:112
本文介绍了使用EPPlus将Excel工作表格式化为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.Net核心应用程序,并且是使用c#的EPPlus库的新手.我有一个填充有数据的数据表,当前正在使用EPPlus将其插入到Excel工作表中. 我想知道是否有一种方法可以在将数据添加到工作表之后而不是之前将工作表格式化为表格? (就像您在excel中使用格式化表格"功能一样)

I have a .Net core application, and i'm new to using the EPPlus Library with c#. I have a data-table filled with data which i'm currently inserting into an excel sheet using EPPlus. I was wondering if there is a way to format the sheet as a table after you added the data to the sheet and not before? (Like when you are using the Format a table function in excel)

我的代码示例仅填充excel工作表:

var excelWorkSheet = excel.Workbook.Worksheets["WorkSheet1"];
using (SqlDataReader dr = cmd.ExecuteReader())
{
    dt.Load(dr);

}
excelWorkSheet.Cells["A1"].LoadFromDataTable(dt, true);

excel.SaveAs(df);

请记住,上面的df变量是文件目录类型,并且数据表中的数据对于每个用户而言都是不同的,因此,两个用户的大小永远不会相同.

please keep in mind the df variable above is of type file directory, and that the data in the data-table will be different for each user so the size will never be the same for two users.

推荐答案

是的,可以使用EPPlus来完成.如果您没有固定的行/列,则可以使用 Dimension .

Yes this can be done with EPPlus. You can use Dimension if you do not have fixed rows/columns.

using OfficeOpenXml.Table;


ExcelWorksheet ws = excel.Workbook.Worksheets[1];

//create a range for the table
ExcelRange range = ws.Cells[1, 1, ws.Dimension.End.Row, ws.Dimension.End.Column];

//add a table to the range
ExcelTable tab = ws.Tables.Add(range, "Table1");

//format the table
tab.TableStyle = TableStyles.Medium2;

这篇关于使用EPPlus将Excel工作表格式化为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆