2

我想将 SQL Server 2008 数据库中的表导出到 Excel 表,其约束与数据库架构中的约束相同。例如,如果在数据库中的列具有整数或 varchar 的数据类型,那么在 excel 表中它应该具有相同的数据类型,并且如果有人试图在列中输入无效数据,则应该在 excel 表本身中显示一条错误消息.

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

4

2 回答 2

1

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

于 2012-04-11T03:38:28.117 回答
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;
    }                                   
}
于 2014-11-05T07:18:27.417 回答