繁体   English   中英

使用C#以编程方式将查询表从MS Access导出到Excel

[英]Programmatically export query table from MS Access to Excel using C#

今天,我有一个可以从Web下载xml数据并将其导出到相应表中的MS Access DB的软件。

在MS Access DB中,我使用表创建了查询,以使列和行符合我希望在Excel中的外观。

当我右键单击新查询表,然后选择“导出到Excel”时,我能够从该查询创建Excel文件。

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

我怎样才能做到这一点?

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

有关其他方面的事情,我也想解决。

我在左侧数字上方得到绿色三角形,请检查图像postimg.org/image/t6tvfw2cz如何从c#中删除。

是否可以使用C#代码格式化表格外观并进行设计?

使用C#代码在标题中添加过滤器是否可行? –法力15小时前

这样的事情应该做到:

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;

另一种解决方案是创建可以在Excel或其他应用程序中打开的CSV文件。 请参阅此函数,该函数返回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;
    }
}

暂无
暂无

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

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