winform中读取了数据库的数据,保存在数据集ds中,想把这些数据导出为一个Excel表格,但是导出时出现问题,代码如下:
            
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "excel files (*.xls)|*.xls|All files (*.*)|*.*";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
                if (xlApp == null)
                {
                    MessageBox.Show("无法创建Excel文档");
                }
                else
                {
                    //Excel.Workbook workbook = xlApp.Workbooks.Add(true);
                    Excel.Workbook workbook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
                    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                    {
                        //先只保存一个表头
                        worksheet.Cells[1, i] = ds.Tables[0].Columns[i].ColumnName;
                    }
                
                    workbook.Saved = true;
                    workbook.SaveCopyAs(sfd.FileName);
                }
               
            }//Excel.Workbook workbook = xlApp.Workbooks.Add(true);
Excel.Workbook workbook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
这两句出现未处理的COMException错误,错误提示为:内存或磁盘空间不足,Microsoft Office Excel 无法再次打开或保存任何文档。• 要想获得更多的可用内存,请关闭不再使用的工作簿或程序。• 要想释放磁盘空间,请删除相应磁盘上不需要的文件。请遇见过此异常的高手门不吝赐教,万分感谢!!!

解决方案 »

  1.   

    程序集中有引用Microsoft.Office.Interop.Excel
    using Excel = Microsoft.Office.Interop.Excel;
      

  2.   

    保存为excel的局限性是只能保存6万5千行,我一般保存为csv。我这个代码是datagridview保存到csv文件的。加个ProgressBar空间,显示进度。
     private bool ExportToCsv(DataGridView dgv, ProgressBar pb)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "CSV(逗号分隔)(*.csv)|*.csv";
                dlg.FilterIndex = 0;
                dlg.RestoreDirectory = true;
                dlg.CreatePrompt = true;
                dlg.Title = "保存为CSV(逗号分隔)文件";            if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Stream myStream;
                    myStream = dlg.OpenFile();
                    StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                    string columnTitle = "";
                    try
                    {
                        //写入列标题   
                        for (int i = 0; i+1 < dgv.ColumnCount; i++)
                        {
                            if (i > 0)
                            {
                                columnTitle += ",";
                            }
                            columnTitle += dgv.Columns[i].HeaderText;
                        }
                        columnTitle.Remove(columnTitle.Length - 1);
                        sw.WriteLine(columnTitle);                    //写入列内容   
                        for (int j = 0; j < dgv.Rows.Count; j++)
                        {
                            string columnValue = "";
                            pb.Value = j * 100 / dgv.Rows.Count;
                            for (int k = 0; k+1 < dgv.Columns.Count; k++)
                            {                            if (k > 0)
                                {
                                    columnValue += ",";
                                }
                                if (dgv.Rows[j].Cells[k].Value == null)
                                {
                                    columnValue += "";
                                }
                                else
                                {
                                    string m = dgv.Rows[j].Cells[k].Value.ToString().Trim();
                                    columnValue += m.Replace(",", ",");
                                }
                            }
                            columnValue.Remove(columnValue.Length - 1);
                            sw.WriteLine(columnValue);
                        }
                        sw.Close();
                        myStream.Close();
                        MessageBox.Show("完成!");
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                        return false;
                    }
                    finally
                    {
                        sw.Close();
                        myStream.Close();
                    }                return true;
                }
                else
                {
                    return false;
                }
            }
      

  3.   

    2楼,我就没占用资源啊,先只是实例化了一个application类而已,其他就没什么东西了,何来释放资源啊
      

  4.   

    新问题,照3楼的代码弄的虽然保存了,但是打开csv的时候弹出一个对话框,上面写的依然是:内存或磁盘空间不足,Microsoft Office Excel 无法再次打开或保存任何文档。• 要想获得更多的可用内存,请关闭不再使用的工作簿或程序。• 要想释放磁盘空间,请删除相应磁盘上不需要的文件。
      

  5.   

    public class ExportToEXCEL
        {
            public void ExportTo(SaveFileDialog saveFileDialog1, Button button3, DataGridView dataGridView1, GroupBox groupBox2, ProgressBar progressBar1)
            {
                bool b1 = true;            //导出数据是否出错
                double n1 = 0;             //进度条显示
                string s1 = "";            //进度条显示
                string sSaveFileName;      //EXCEL文件名
                saveFileDialog1.Reset();
                saveFileDialog1.DefaultExt = "xlsx";
                saveFileDialog1.Filter = "xlsx|*.xlsx";
                saveFileDialog1.ShowDialog();
                sSaveFileName = saveFileDialog1.FileName;
                if (string.IsNullOrEmpty(sSaveFileName) == false)
                {
                    try
                    {
                        button3.Enabled = false;
                        if (dataGridView1.Rows.Count > 0)
                        {
                            groupBox2.Text = "请等待,正在导出数据至EXCEL... ...";
                            groupBox2.Refresh();
                            Microsoft.Office.Interop.Excel.Application Excel1 = new Microsoft.Office.Interop.Excel.Application();
                            Excel1.Application.Workbooks.Add(true);
                            Excel1.Visible = false;
                            for (int i = 0; i < dataGridView1.ColumnCount; i++)
                            {
                                if (dataGridView1.Columns[i].Visible == true)
                                    Excel1.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
                            }
                            for (int i1 = 0; i1 < dataGridView1.RowCount; i1++)
                            {
                                Application.DoEvents();
                                n1 = (double)(i1 + 1) / dataGridView1.RowCount * 100;
                                progressBar1.Value = (int)n1;
                                s1 = progressBar1.Value.ToString() + "%";
                                groupBox2.Text = "完成百分比:" + s1;
                                for (int i2 = 0; i2 < dataGridView1.ColumnCount; i2++)
                                {
                                    if (dataGridView1.Columns[i2].Visible == true)
                                        Excel1.Cells[i1 + 2, i2 + 1] ="'" + dataGridView1.Rows[i1].Cells[i2].Value;
                                }
                            }
                            //如果文件存在,则先删除该文件
                            if (System.IO.File.Exists(sSaveFileName))
                            {
                                System.IO.File.Delete(sSaveFileName);
                            }
                            Excel1.ActiveWorkbook.Close(true, sSaveFileName, Missing.Value);
                            Excel1.Quit();
                            groupBox2.Text = "数据导出EXCEL完成";
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        b1 = false;
                    }
                    finally
                    {
                        if (b1 == true)
                            MessageBox.Show("导出EXCEL文件成功完成", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        button3.Enabled = true;
                    }
                }
            }
        }我项目中用的导出数据至EXCEL文件的类,用了一年多了,没问题
      

  6.   

    解决了,NND,是office的问题,重装了office,然后打上sp2补丁就好了,哎,不过还是学习了非常非常多,呵呵,3Q all
      

  7.   

    还导入了一个注册表,不过好像不是解决这个问题的,但有可能也有关系。具体就是出现****.tlb提示框的错误的解决办法,提供的是一个注册表解决方法
      

  8.   

    混分,谁让你不结贴的,但是,怎么导出的电子表表格中要含有sheet1,sheet2....呢?
      

  9.   

    我靠,我居然这里有一个帖子,我都忘了,不好意思各位,我那时候在csdn上发帖,根本不知道结贴这么一回事,今天才刚看见,结了结了,sorry,sorry,sorry,sorry,sorry,sorry