设置表格样式。 C#-封闭的XML

时间:2018-12-06 15:11:28

标签: c# excel xlsx closedxml

我正在寻找一种将表格样式应用于excel文件中插入的数据的方法。 我用图书馆封闭的xml 我该怎么办?

我想要的样品表

enter image description here

        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;
                                }}}}}}}

1 个答案:

答案 0 :(得分:2)

基于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;