将DataGridView导出到Excel文件

时间:2014-03-07 00:49:06

标签: c# datagridview

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

当我尝试将数据导出到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 ="&#34 ;;             //导出标题:             string sHeaders ="";

        for (int j = 0; j < dGV.Columns.Count; j++)
            sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
        stOutput += sHeaders + "\r\n";
        // 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 + "\r\n";
        }
        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();
    }  

1 个答案:

答案 0 :(得分:0)

尝试导出到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);