Экспорт сетки в Excel в приложении Windowsforms - PullRequest
0 голосов
/ 08 марта 2010

Мне нужен код для экспорта gridview, чтобы преуспеть в приложении Windows Forms ... Кто-нибудь может помочь с этим кодом?

Ответы [ 3 ]

2 голосов
/ 08 марта 2010

Это длинный ответ, но здесь он идет.

Попробуйте создать класс ниже ...

 public class ExcelWriter : IDisposable
{
    private XmlWriter _writer;

    public enum CellStyle { General, Number, Currency, DateTime, ShortDate };

    public void WriteStartDocument()
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"");
        _writer.WriteStartElement("ss", "Workbook", "urn:schemas-microsoft-com:office:spreadsheet");
        WriteExcelStyles();
    }

    public void WriteEndDocument()
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteEndElement();
    }

    private void WriteExcelStyleElement(CellStyle style)
    {
        _writer.WriteStartElement("Style", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteAttributeString("ID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
        _writer.WriteEndElement();
    }

    private void WriteExcelStyleElement(CellStyle style, string NumberFormat)
    {
        _writer.WriteStartElement("Style", "urn:schemas-microsoft-com:office:spreadsheet");

        _writer.WriteAttributeString("ID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
        _writer.WriteStartElement("NumberFormat", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteAttributeString("Format", "urn:schemas-microsoft-com:office:spreadsheet", NumberFormat);
        _writer.WriteEndElement();

        _writer.WriteEndElement();

    }

    private void WriteExcelStyles()
    {
        _writer.WriteStartElement("Styles", "urn:schemas-microsoft-com:office:spreadsheet");

        WriteExcelStyleElement(CellStyle.General);
        WriteExcelStyleElement(CellStyle.Number, "General Number");
        WriteExcelStyleElement(CellStyle.DateTime, "General Date");
        WriteExcelStyleElement(CellStyle.Currency, "Currency");
        WriteExcelStyleElement(CellStyle.ShortDate, "Short Date");

        _writer.WriteEndElement();
    }

    public void WriteStartWorksheet(string name)
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteStartElement("Worksheet", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteAttributeString("Name", "urn:schemas-microsoft-com:office:spreadsheet", name);
        _writer.WriteStartElement("Table", "urn:schemas-microsoft-com:office:spreadsheet");
    }

    public void WriteEndWorksheet()
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteEndElement();
        _writer.WriteEndElement();
    }

    public ExcelWriter(string outputFileName)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        _writer = XmlWriter.Create(outputFileName, settings);
    }

    public void Close()
    {
        if (_writer == null) throw new NotSupportedException("Already closed.");

        _writer.Close();
        _writer = null;
    }

    public void WriteExcelColumnDefinition(int columnWidth)
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteStartElement("Column", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteStartAttribute("Width", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteValue(columnWidth);
        _writer.WriteEndAttribute();
        _writer.WriteEndElement();
    }

    public void WriteExcelUnstyledCell(string value)
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteStartElement("Cell", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteStartElement("Data", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "String");
        _writer.WriteValue(value);
        _writer.WriteEndElement();
        _writer.WriteEndElement();
    }

    public void WriteStartRow()
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteStartElement("Row", "urn:schemas-microsoft-com:office:spreadsheet");
    }

    public void WriteEndRow()
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteEndElement();
    }

    public void WriteExcelStyledCell(object value, CellStyle style)
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        _writer.WriteStartElement("Cell", "urn:schemas-microsoft-com:office:spreadsheet");
        _writer.WriteAttributeString("StyleID", "urn:schemas-microsoft-com:office:spreadsheet", style.ToString());
        _writer.WriteStartElement("Data", "urn:schemas-microsoft-com:office:spreadsheet");
        switch (style)
        {
            case CellStyle.General:
                _writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "String");
                break;
            case CellStyle.Number:
            case CellStyle.Currency:
                _writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "Number");
                break;
            case CellStyle.ShortDate:
            case CellStyle.DateTime:
                _writer.WriteAttributeString("Type", "urn:schemas-microsoft-com:office:spreadsheet", "DateTime");
                break;
        }
        _writer.WriteValue(value);
        //  tag += String.Format("{1}\"><ss:Data ss:Type=\"DateTime\">{0:yyyy\\-MM\\-dd\\THH\\:mm\\:ss\\.fff}</ss:Data>", value,

        _writer.WriteEndElement();
        _writer.WriteEndElement();
    }

    public void WriteExcelAutoStyledCell(object value)
    {
        if (_writer == null) throw new NotSupportedException("Cannot write after closing.");

        //write the <ss:Cell> and <ss:Data> tags for something
        if (value is Int16 || value is Int32 || value is Int64 || value is SByte ||
            value is UInt16 || value is UInt32 || value is UInt64 || value is Byte)
        {
            WriteExcelStyledCell(value, CellStyle.Number);
        }
        else if (value is Single || value is Double || value is Decimal) //we'll assume it's a currency
        {
            WriteExcelStyledCell(value, CellStyle.Currency);
        }
        else if (value is DateTime)
        {
            //check if there's no time information and use the appropriate style
            WriteExcelStyledCell(value, ((DateTime)value).TimeOfDay.CompareTo(new TimeSpan(0, 0, 0, 0, 0)) == 0 ? CellStyle.ShortDate : CellStyle.DateTime);
        }
        else
        {
            WriteExcelStyledCell(value, CellStyle.General);
        }
    }

    #region IDisposable Members

    public void Dispose()
    {
        if (_writer == null)
            return;

        _writer.Close();
        _writer = null;
    }

    #endregion
}

А затем в вашем приложении winform создайте следующую функцию ...

        public static void ExcelExport(DataTable data, String fileName, bool openAfter)
    {
        //export a DataTable to Excel
        DialogResult retry = DialogResult.Retry;
        OpenFileDialog openFileDialog = new OpenFileDialog();

        while (retry == DialogResult.Retry)
        {
            try
            {
                using (ExcelWriter writer = new ExcelWriter(fileName))
                {
                    writer.WriteStartDocument();

                    // Write the worksheet contents
                    writer.WriteStartWorksheet("Sheet1");

                    //Write header row
                    writer.WriteStartRow();
                    foreach (DataColumn col in data.Columns)
                        writer.WriteExcelUnstyledCell(col.Caption);
                    writer.WriteEndRow();

                    //write data
                    foreach (DataRow row in data.Rows)
                    {
                        writer.WriteStartRow();
                        foreach (object o in row.ItemArray)
                        {
                            if (Convert.IsDBNull(o))
                            {
                                writer.WriteExcelUnstyledCell(String.Empty);
                            }
                            else
                            {
                                writer.WriteExcelAutoStyledCell(o);
                            }
                        }
                        writer.WriteEndRow();
                    }

                    // Close up the document
                    writer.WriteEndWorksheet();
                    writer.WriteEndDocument();
                    writer.Close();
                    if (openAfter)
                    {
                        openFileDialog.FileName = fileName;
                        //openFileDialog.ShowDialog();
                            openFileDialog.OpenFile();
                    }
                    retry = DialogResult.Cancel;

                }
            }
            catch (Exception myException)
            {
                retry = MessageBox.Show(myException.Message, "Excel Export", MessageBoxButtons.RetryCancel, MessageBoxIcon.Asterisk);
            }
        }

    }

Наконец, чтобы использовать это, попробуйте это из вашего желаемого события / метода ...

ExcelExport('YourdDataTable',"NameOfYourFile.xml",true);
1 голос
/ 08 марта 2010
0 голосов
/ 28 мая 2015

Согласно ответу @Refracted Paladin, единственная ошибка, которую я нашел, это

Xml type 'List of xdt:untypedAtomic' does not support a conversion from Clr type 'Guid' to Clr type 'String'. 

Именно из-за его кода не проверяют GUID. Поэтому я немного изменил его код, чтобы вы также могли проверить мой ответ.

Type _Type = o.GetType();
if (_Type.Equals(typeof(System.Guid)))
{
    string GuidValueStr = Convert.ToString(o);                                    
    writer.WriteExcelAutoStyledCell((object)GuidValueStr);      
}

После того, как я изменил его функцию

public static void ExcelExport(DataTable data, String fileName)
{   
    try
    {
        using (ExcelWriter_StackOverFlow writer = new ExcelWriter_StackOverFlow(fileName))
        {
            writer.WriteStartDocument();

            // Write the worksheet contents
            writer.WriteStartWorksheet("Sheet1");

            //Write header row
            writer.WriteStartRow();
            foreach (DataColumn col in data.Columns)
                writer.WriteExcelUnstyledCell(col.Caption);
            writer.WriteEndRow();

            //write data
            foreach (DataRow row in data.Rows)
            {
                writer.WriteStartRow();
                foreach (object o in row.ItemArray)
                {
                    if (o.ToString().Length <= 0)
                    {
                        writer.WriteExcelAutoStyledCell(string.Empty);
                    }
                    else {
                        Type _Type = o.GetType();
                        if (_Type.Equals(typeof(System.Guid)))
                        {
                            string GuidValueStr = Convert.ToString(o);                                    
                            writer.WriteExcelAutoStyledCell((object)GuidValueStr);
                            continue;
                        }
                        writer.WriteExcelAutoStyledCell(o);
                    }
                }
                writer.WriteEndRow();
            }

            // Close up the document
            writer.WriteEndWorksheet();
            writer.WriteEndDocument();
            writer.Close();

        }
    }
    catch (Exception myException)
    {
        throw myException;
    }            
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...