使用c#将SQL Server 2008数据库中的表导出为ex​​cel

时间:2012-04-10 13:56:32

标签: c# asp.net sql-server export-to-excel

我想将一个表从SQL Server 2008数据库导出到Excel工作表,其中包含与数据库模式相同的约束。 例如,如果在数据库中,列的数据类型为integer或varchar 然后在excel表中它应该具有相同的数据类型,并且如果有人试图在列中输入无效数据,则应该在excel表单中显示一条错误消息。

我找到了一个代码脚本here,但它没有实现约束。

2 个答案:

答案 0 :(得分:1)

签出EPPlus它能够将数据表转换为XLSX,您可以将数据验证应用于列以模拟Excel中的“数据类型”。

答案 1 :(得分:0)

//这是我用于SQL Server到Excel数据库转储的代码 //试试吧

private void button1_Click_2(object sender, EventArgs e)
{
    DataSet DtSetmatch = new DataSet();
    SQL.DataTable dttt = new SQL.DataTable();
    string selectedTable = cmbImportItemList.Text;
    using (SqlDataAdapter adater = new SqlDataAdapter("Select * from "+selectedTable, new SqlConnection(Properties.Settings.Default.connectionstring2)))
    {
        adater.Fill(dttt);
    }          
    Excel.Application oXL;
    Excel._Workbook oWB;
    Excel._Worksheet oSheet;
    oXL = new Excel.Application();
    oXL.Visible = true;            
    oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
    oSheet = (Excel._Worksheet)oWB.ActiveSheet;
    SQL.DataTable dtCategories = dttt.DefaultView.ToTable(true,dttt.Columns[0].ToString());

    foreach (SQL.DataRow category in dtCategories.Rows)
    {
        oSheet = (Excel._Worksheet)oXL.Worksheets.Add();
        oSheet.Name = category[0].ToString()
            .Replace(" ", "")
            .Replace("  ", "")
            .Replace("/", "")
            .Replace("\\", "")
            .Replace("*", "");                    
        string[] colNames = new string[dttt.Columns.Count];
        int col = 0;
        try
        {
            foreach (SQL.DataColumn dc in dttt.Columns) colNames[col++] = dc.ColumnName;////dc.ColumnName;
            char lastColumn = (char)(65 + dttt.Columns.Count - 1);
            oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
            oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
            oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;                            
            SQL.DataRow[] dr = dttt.Select(string.Format(""));
            string[,] rowData = new string[dr.Count<SQL.DataRow>(), dttt.Columns.Count];                            
            int rowCnt = 0;
            int redRows = 2;
            foreach (SQL.DataRow row in dr)
            {                                
                for (col = 0; col < dttt.Columns.Count; col++)
                {
                    rowData[rowCnt, col] = row[col].ToString();                                   
                }
                redRows++;
                rowCnt++;                             
            }                            
            oSheet.get_Range("A2", lastColumn + rowCnt.ToString()).Value2 = rowData;
            oXL.Visible = true;
            oXL.UserControl = true;
            try
            {
                oWB.SaveAs("C://Products.xlsx", AccessMode: Excel.XlSaveAsAccessMode.xlShared);
            }
            catch
            {
            }
            break;
        }
        catch (Exception aee)
        {
        }
        break;
    }                                   
}