Excel(exl,xls),PDF,Access,CSV,电子表格,C#导出 [英] Excel(exl,xls), PDF,Access, CSV,Spreedsheet, Export in C#

查看:84
本文介绍了Excel(exl,xls),PDF,Access,CSV,电子表格,C#导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview和一个数据集,
我希望通过单击导出按钮以Excel,CSV,PDF格式导出datagridview行..

我对此一无所知..请告诉我逐步操作以完成此操作..

我必须在工具箱中包含哪些COM组件..
.net 2008中的Excel应用程序类是什么.c#

I have a datagridview and a dataset,
i wish to export the datagridview rows in Excel,CSV,PDF, format by clicking on the export button..

i know nothing about it....please tell me step by step procedure to get it done..

what com component i have to include in my toolbox..
and what is the Excel Application Class..in .net 2008 c#

推荐答案

您可以使用以下代码将数据表写入文本文件

在下面的示例中,FieldDelimiter是类中的const-您可以选择要使用的内容(例如,pipe |或tilde〜或逗号)

You can write a datatable to a text file with code as follows

In the example below, FieldDelimiter is a const in a class - you can choose what to use (e.g maybe pipe | or tilde ~ or comma ,)

private static string WriteToTextFile(System.Data.DataTable data)
{
    string fileName = System.IO.Path.GetTempFileName();
    using (FileStream fs = new System.IO.FileStream(fileName, FileMode.Create))
    {
        using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Unicode))
        {
            string columnHeaders = string.Empty;
            for (int i = 0; i < data.Columns.Count; ++i)
            {
                columnHeaders += data.Columns[i].Caption + (i < data.Columns.Count - 1 ? FieldDelimiter : string.Empty);
            }
            // Write the field names (headers) as the first line in the text file.
            sw.WriteLine(columnHeaders);
            foreach (DataRow row in data.Rows)
            {
                for (int i = 0; i < data.Columns.Count; ++i)
                {
                    string s = row[i].ToString() + (i < data.Columns.Count - 1 ? FieldDelimiter : string.Empty);
                    sw.Write(s);
                }
                sw.WriteLine();
            }
            sw.Flush(); // Write the buffered data to the filestream.
            sw.Close();
            // Close the FileStream.
            fs.Close();
            return fileName;
        }
    }
}



导出为ex​​cel可以通过多种方式完成.一种是简单地使Excel自动化并将DataTable的内容写入excel工作簿.例如在这里看看

http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx [



Exporting to excel can be done in a number of ways. One is simply to automate Excel and write the contents of the DataTable into the excel workbook. e.g have a look here

http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx[^]

I find this method has a performance hit, I get better performance by using the WriteToTextFile code above, then importing the entire text file into Excel.

e.g something like

private static Workbook CreateWorkbookFromTextfile(Microsoft.Office.Interop.Excel.Application excel, string fileName)
{
    Workbooks books = null;
    Workbook workbook = null;

    try
    {
        books = excel.Workbooks;

        books.OpenText(
            fileName,
            XlPlatform.xlWindows, 1,
            XlTextParsingType.xlDelimited,
            XlTextQualifier.xlTextQualifierDoubleQuote,
            false, false, false, false, false, true,
            FieldDelimiter, Missing.Value,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        workbook = books.get_Item(1);
        return workbook;
    }
    finally
    {
        #region COM Interop Cleanup Code
        System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
        #endregion
    }

}



这应该可以帮助您开始使用Excel& amp; CSV文件,但是PDF是另一个问题



That should help you get started with Excel & CSV file, PDF is a different question though


这篇关于Excel(exl,xls),PDF,Access,CSV,电子表格,C#导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆