首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

下面这段导出为excel的思路哪位高手能解释一下吗

2012-05-21 
下面这段导出为excel的思路谁能解释一下吗C# codepublic void print(DataGridView dataGridView1){//导出

下面这段导出为excel的思路谁能解释一下吗

C# code
public void print(DataGridView dataGridView1)        {            //导出到execl                  try            {                SaveFileDialog saveFileDialog = new SaveFileDialog();                saveFileDialog.Filter = "导出Excel (*.xls)|*.xls";                saveFileDialog.FilterIndex = 0;                saveFileDialog.RestoreDirectory = true;                saveFileDialog.CreatePrompt = true;                saveFileDialog.Title = "导出文件保存路径";                saveFileDialog.ShowDialog();                string strName = saveFileDialog.FileName;                if (strName.Length != 0)                {                                        if (dataGridView1.Rows.Count == 0)                    {                        return;                    }                     System.Reflection.Missing miss = System.Reflection.Missing.Value;                                       Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();                    excel.Application.Workbooks.Add(true);                    excel.Visible = false;                      if (excel == null)                    {                        MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);                        return;                    }                    Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;                    Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));                    Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;                                           for (int i = 0; i < dataGridView1.Columns.Count; i++)                    {                        excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;                      }                                         for (int i = 0; i < dataGridView1.Rows.Count ; i++)                    {                        for (int j = 0; j < dataGridView1.Columns.Count; j++)                        {                            if (dataGridView1[j, i].ValueType == typeof(string))                            {                                excel.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();                            }                            else                            {                                excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();                            }                        }                    }                    sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);                    book.Close(false, miss, miss);                    books.Close();                    excel.Quit();                    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);                    System.Runtime.InteropServices.Marshal.ReleaseComObject(book);                    System.Runtime.InteropServices.Marshal.ReleaseComObject(books);                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);                    GC.Collect();                    MessageBox.Show("数据已经成功导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                     System.Diagnostics.Process.Start(strName);                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "错误提示");            }        }

特别是System.Runtime.InteropServices.Marshal.ReleaseComObject这几句是什么意思?

[解决办法]
释放非托管资源
------解决方案--------------------


excel跟dataGridView的相应单元格一一对应,结构相同
[解决办法]
ReleaseComObject
释放调用的com组件的资源
因为你调用的是com组件.
[解决办法]
这个是中规中矩的到处DGV,最好是导出来看下,在比对代码琢磨下

我前几天做了个导出Excel的,格式比较复杂,
一个单元格设置标题换行多个字体,附加图片,复杂公式计算,列自动筛选,动态加载数据显示列滚动
多个WorkSheet导出数据源,保存文件名设置

很多资料网上找不到,这个导出Excel做的很有成就感

热点排行