在excel中将tableStyle提供给我的excel表

时间:2018-04-16 14:35:28

标签: c# excel-dna

我使用excel dna在excel文件中显示存储过程的结果,我想给excel表的标题提供表格样式,但我没有找到方法来执行此操作,我找到的例子与我的情况不同,这是我的代码,它可以解释更多:

   public static object loadViewData(string date)
    {
        DateTime dt = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        object[,] resultFromDB = getViewDataFromDB(dt);           
        object res= ArrayResizer.displayResultInExcelFile(resultFromDB);

        return res;
    }

getViewDataFromDB从sql Server返回数据,displayResultInExcelFile将数据显示到excel文件中,这就是它的代码:

            public static object displayResultInExcelFile(object[,]array)
    {
        var caller = Excel(xlfCaller) as ExcelReference;
        if (caller == null)
            return array;

        int rows = array.GetLength(0);
        int columns = array.GetLength(1);
        if (rows == 0 || columns == 0)
            return array;

        var rowLast = caller.RowFirst + rows - 1;
        var columnLast = caller.ColumnFirst + columns - 1;
        if (rowLast > ExcelDnaUtil.ExcelLimits.MaxRows - 1 ||
         columnLast > ExcelDnaUtil.ExcelLimits.MaxColumns - 1)
        {
            return ExcelError.ExcelErrorValue;
        }

        ExcelAsyncUtil.QueueAsMacro(
            delegate
            {
                var newTarget = new ExcelReference(caller.RowFirst + 1, rowLast + 1, caller.ColumnFirst, columnLast, caller.SheetId);
                newTarget.SetValue(array);
            } );
        return array;
    }

我可以将一个tableStyle添加到我的excel文件中吗?我想要像

这样的东西

TableStyle =" TableStyleMedium9&#34 ;;

由于

1 个答案:

答案 0 :(得分:0)

最后我找到了如何在Excel工作表中添加表格样式,

 public static object displayResultInExcelFile(object[,]array)
{
    var caller = Excel(xlfCaller) as ExcelReference;
    if (caller == null)
        return array;

    int rows = array.GetLength(0);
    int columns = array.GetLength(1);
    if (rows == 0 || columns == 0)
        return array;

    var rowLast = caller.RowFirst + rows - 1;
    var columnLast = caller.ColumnFirst + columns - 1;
    if (rowLast > ExcelDnaUtil.ExcelLimits.MaxRows - 1 ||
     columnLast > ExcelDnaUtil.ExcelLimits.MaxColumns - 1)
    {
        return ExcelError.ExcelErrorValue;
    }

    ExcelAsyncUtil.QueueAsMacro(
        delegate
        {
            var newTarget = new ExcelReference(caller.RowFirst + 1, rowLast + 1, caller.ColumnFirst, columnLast, caller.SheetId);
Microsoft.Office.Interop.Excel.Application oxl = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");
            oxl.Visible = true;               
            Workbook wrkbk = oxl.ActiveWorkbook;
            string wbkname =wrkbk.Name;
            Worksheet wrksht = wrkbk.ActiveSheet ;
            string wrksheetname = oxl.ActiveSheet.Name;               
            object cell1 = wrksht.Cells[newTarget.RowFirst+1, newTarget.ColumnFirst];

            object cell2 = wrksht.Cells[newTarget.RowFirst+1
                , newTarget.ColumnLast];
            Range headerRange = wrksht.get_Range(cell1, cell2);
            wrksht.ListObjects.AddEx(XlListObjectSourceType.xlSrcRange, headerRange, Type.Missing,XlYesNoGuess.xlYes,Type.Missing).Name= "MyTableStyle";
            wrksht.ListObjects.get_Item("MyTableStyle").TableStyle = "TableStyleMedium9";

            newTarget.SetValue(array);
        } );
    return array;
}