将DataGridView导出到Excel文件

你能说我,我怎么能保存DataGridView内容格式化单元格Backcolor / Forecolor的Exel文件? 我有这样的DGV:

当我尝试将数据导出到Excel时,我获取了数据,但没有使用Forecolor,我尝试了几种方法,但它不起作用! 事先谢谢!

private void btnExport_Click(object sender,EventArgs e){

        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel Documents (*.xls)|*.xls";
        sfd.FileName = "export.xls";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            //ToCsV(dataGridView1, @"c:export.xls");
            ToCsV1(dataGridView1, sfd.FileName);
        }  

}

private void ToCsV1(DataGridView dGV,string filename){string stOutput =“”; //导出标题:string sHeaders =“”;

        for (int j = 0; j < dGV.Columns.Count; j++)
            sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "t";
        stOutput += sHeaders + "rn";
        // Export data.
        for (int i = 0; i < dGV.RowCount - 1; i++)
        {
            string stLine = "";
            for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
                stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "t";
            stOutput += stLine + "rn";
        }
        Encoding utf16 = Encoding.GetEncoding(1254);
        byte[] output = utf16.GetBytes(stOutput);
        FileStream fs = new FileStream(filename, FileMode.Create);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(output, 0, output.Length); //write the encoded file
        bw.Flush();
        bw.Close();
        fs.Close();
    }  

尝试导出到ExcelXML

例:

 using (var writer = new StreamWriter(fileDialog.OpenFile(), Encoding.UTF8))
                    {
                        if (format == "XML")
                        {
                            writer.WriteLine("<?xml version="1.0" encoding="utf-8"?>");
                            writer.WriteLine("<?mso-application progid="Excel.Sheet"?>");
                            writer.WriteLine("<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet">");
                            writer.WriteLine("<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">");
                            writer.WriteLine("<Author></Author>");
                            writer.WriteLine("<Created>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</Created>");
                            writer.WriteLine("<LastSaved>" + DateTime.Now.ToLocalTime().ToLongDateString() + "</LastSaved>");
                            writer.WriteLine("<Company></Company>");
                            writer.WriteLine("<Version>12.00</Version>");
                            writer.WriteLine("</DocumentProperties>");

                            if (showGroupHeaders)
                                AddStyles(writer, groupDesc.Count());

                            writer.WriteLine(
                                "<Worksheet ss:Name="Silverlight Export" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">");
                            writer.WriteLine("<Table>");
                        }

                        writer.Write(text.ToString());
                        if (format == "XML")
                        {
                            writer.WriteLine("</Table>");
                            writer.WriteLine("</Worksheet>");
                            writer.WriteLine("</Workbook>");
                        }

                        writer.Close();
                    }

AddStyle像这样:

    private static void AddStyles(StreamWriter writer, int groupCount)
            {
                writer.WriteLine("<Styles>");

                for (int i = 0; i < groupCount; i++)
                {
                    int shift = i % _headerColor.Count();
                    var color = _headerColor[shift];
                    writer.WriteLine("<Style ss:ID="st{0}">", i + 1);
                    writer.WriteLine("<Interior ss:Color="#{0:X2}{1:X2}{2:X2}" ss:Pattern="Solid"/>", color.R, color.G, color.B);
                    writer.WriteLine("</Style>");
                }

                writer.WriteLine("</Styles>");
            }

并设置单元格背景:

string.Format("<Cell ss:StyleID="st{0}"><Data ss:Type="String">{1}</Data></Cell>", style, data);
链接地址: http://www.djcxy.com/p/71163.html

上一篇: Export DataGridView to Excel File

下一篇: Accessing string from another Form