【问题标题】:Set table style. C# - closed xml设置表格样式。 C# - 关闭的 xml
【发布时间】:2018-12-06 15:11:27
【问题描述】:

我正在寻找一种将表格样式应用于 Excel 文件中插入的数据的方法。 我使用库关闭的 xml 我该怎么做?

我要获取的示例表

        using (XLWorkbook wb = new XLWorkbook(excel))
        {   IXLWorksheet ws = wb.Worksheets.Last();
            string Qry;
            using (SqlCommand cmd = new SqlCommand(Qry, sqlConn))
            {
                sqlConn.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    System.Data.DataTable schemaTable = dr.GetSchemaTable();
                    int i = 1;
                    foreach (DataRow rowt in schemaTable.Rows)
                    {
                    while (dr.Read())
                    {
                        row++;
                        for (int j = 0; j < dr.FieldCount; j++)
                        {
                            if (!dr.IsDBNull(j))
                            {
                                switch (dr.GetDataTypeName(j))
                                {
                                    case "Varchar2":
                                        string s = dr.GetString(j);
                                        if (s.Substring(0, 1) == "=")
                                            s = " " + s;
                                        ws.Cell(row, j + 1).Value = s;
                                        break;
                                    case "nvarchar":
                                        ws.Cell(row, j + 1).Value = dr.GetString(j);
                                        break;
                                    default:
                                        break;
                                }}}}}}}

【问题讨论】:

  • 您可能还想查看将 SQL 数据集转储到 ClosedXML 中的现有函数,例如 cell.InsertData

标签: c# excel xlsx closedxml


【解决方案1】:

基于ClosedXML documentation:

// create the Excel workbook
var wb = new XLWorkbook(); 

// creates the worksheet
var ws = wb.AddWorksheet("Sheet1"); 

// the range for which you want to add a table style
var range = ws.Range(1, 1, 5, 5); 

// create the actual table
var table = range.CreateTable();

// apply style
namesTable.Theme = XLTableTheme.TableStyleLight12;

【讨论】:

  • 表格样式列表在这里:link
猜你喜欢
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多