【问题标题】:Programmatically export query table from MS Access to Excel using C#使用 C# 以编程方式将查询表从 MS Access 导出到 Excel
【发布时间】:2013-12-10 14:22:18
【问题描述】:

今天我有一个软件,它可以从网上下载 xml 数据并将其导出到适当表中的 MS Access DB。

在 MS Access DB 中,我创建了一个查询,使用表创建列和行,就像我希望它在 Excel 中一样。

当我右键单击我的新查询表并选择导出到 Excel 时,我可以从该查询创建一个 Excel 文件。

基本上我想做的是扩展我的软件,以便我可以使用 C# 以编程方式将查询导出到 Excel。

我该怎么做?

---------------------------

其他相关的事情我也想解决。

我在左侧的数字上方出现绿色三角形,请查看图片postimg.org/image/t6tvfw2cz 如何从 c# 中删除。

是否可以使用 c# 代码格式化表格外观和设计?

是否可以使用 c# 代码向标题添加过滤器? – 法力 15 小时前

【问题讨论】:

  • 我建议创建新问题来询问您的后续行动。如果需要,您可以在相关问题之间建立链接。

标签: c# database excel ms-access


【解决方案1】:

应该这样做:

private static void ExportQuery(string databaseLocation, string queryNameToExport, string locationToExportTo)
{
    var application = new Application();
    application.OpenCurrentDatabase(databaseLocation);
    application.DoCmd.TransferSpreadsheet(AcDataTransferType.acExport, AcSpreadSheetType.acSpreadsheetTypeExcel12,
                                          queryNameToExport, locationToExportTo, true);
    application.CloseCurrentDatabase();
    application.Quit();
    Marshal.ReleaseComObject(application);
}

你可以这样称呼它:

ExportQuery(@"C:\blah\blah.accdb", "myQuery", @"C:\blah\blah.xlsx");

请务必添加这些 using 语句:

using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Access;

【讨论】:

  • 我缺少对 Microsoft.Office.Interop.Access 的程序集引用。我在哪里可以找到它?
  • Oki,所以我找到了。立即测试您的解决方案。
  • 嗨,我现在已经实现并测试了一些,但收到以下错误,“Excel 无法打开文件 [文件名],因为文件格式或文件扩展名无效”。 excel文件生成了,但是excel文件打不开。
  • @Mana 以上会根据您的 Excel 版本而有所不同,您需要使用文件扩展名和 AcSpreadSheetType 枚举,直到它起作用。
  • 好东西,TransferSpreadsheet 方法的文档是here,它描述了不同的参数以及它们如何影响输出。
【解决方案2】:

另一种解决方案是创建一个 CSV 文件,该文件可以在 Excel 或其他应用程序中打开。请参阅返回 StringBuilder 的此函数。然后可以将其保存为文件并通过以下代码打开:-

  string _filename = "a path" + "name.csv";
  File.WriteAllText(_filename, TheReturnedStringBuilder.ToString());
  System.Diagnostics.Process.Start(_filename);

CSV 创建

    /// <summary>
    /// Nvest Development Solutions
    /// Export the SQL data into a comma separated file
    /// </summary>
    /// <param name="ConnectionString">Valid SQL Server connection string</param>
    /// <param name="Sql">A valid SQL statement</param>
    /// <param name="TimeOut">A timeout specified in seconds</param>
    /// <returns>A stringbuilder with comma separated data</returns>
    public static StringBuilder ExportToCSVFormat(string ConnectionString, string Sql, int TimeOut = 30)
    {
        StringBuilder csv = new StringBuilder();
        string s = "";
        DataTable dt = SQL.BuildTableFromSQL(ConnectionString, Sql, TimeOut);

        //Add the titles
        foreach (DataColumn col in dt.Columns)
        {
            s += "\"" + col.ColumnName + "\", ";
        }
        if (s.Length > 1)
        {
            s = s.Remove(s.Length - 2);
        }
        csv.AppendLine(s);

        //Add the data
        foreach (DataRow row in dt.Rows)
        {
            object[] param = new object[dt.Columns.Count];
            int j = 0;
            s = "";

            foreach (DataColumn col in dt.Columns)
            {
                s += "{" + j + "";

                if (col.DataType == typeof(int) || col.DataType == typeof(long) || col.DataType == typeof(double))
                {
                    s += ":0},";
                }
                else
                {
                    s += ":},";
                }

                param[j] = row[col.ToString()];

                j++;
            }

            csv.AppendLine(string.Format(s, param));
        }

        return csv;
    }
}

【讨论】:

  • 我一定误解了,因为我希望 C# 代码将数据输出到 Excel,这就是上面所做的。不过没关系 - 希望您能找到解决方案。
  • 您的解决方案没有错,它可能会起作用,只是这样,我必须重新编程整个软件,以便它将数据导出到 .csv 文件而不是 ms访问数据库。您的解决方案不相关的其他原因是因为访问中有很多表,我正在使用这些表在访问中创建查询表。我想要在 Excel 中输出的正是这些查询表。开始重新编程所有这些以使用 .csv 是没有意义的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2017-08-28
  • 1970-01-01
相关资源
最近更新 更多