简体   繁体   English

C# 数据表 linq 条件检查

[英]C# Datatable linq condiction check

I have created the function shown below.我创建了如下所示的 function。 Everything is working fine, the challenge now, is that I have some scenarios where it would be ideal to have the constructor 'RadGridView grid' as a Datatable.一切正常,现在的挑战是我有一些场景,将构造函数“RadGridView grid”作为数据表是理想的。 I'm looking for a constructor that can take the input type of 'RadGridView' or type of 'Datatable' - a kind of "universal" constructor, if that is possiple?我正在寻找一个可以采用“RadGridView”输入类型或“Datatable”类型的构造函数——一种“通用”构造函数,如果可能的话?

public static void Export(RadGridView grid, string sheetName)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Excel file |*.xlsx";
            saveFileDialog1.Title = "Save";
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.FileName != "")
            {
                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                app.Visible = false;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                worksheet.Name = sheetName;
                Microsoft.Office.Interop.Excel.Range aRange = worksheet.UsedRange;
                workbook.Worksheets[1].Cells.NumberFormat = "@";

                for (int i = 1; i < grid.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = grid.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < grid.Rows.Count; i++)
                {
                    for (int j = 0; j < grid.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = grid.Rows[i].Cells[j].Value.ToString();
                    }
                }

                aRange.Value = aRange.Value;
                aRange.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

                worksheet.Columns.AutoFit();
                workbook.SaveAs(saveFileDialog1.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                app.Quit();
            }
        }

Thanks in advance!提前致谢!

I solved this by using the 'dynamic' keyword.我通过使用“动态”关键字解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM