private void ExportToExcel(string FileName)
        {
            if (FileName.IndexOf(".") <= 0)
            {
                FileName = FileName + ".xls";
            }
            else
            {
                string[] str = FileName.Split('.');
                if (str.Length > 2)
                {
                    FileName = str[0] + str[str.Length - 1];
                }
            }
            Excel.Application app;
            try
            {
                app = new Excel.Application();
            }
            catch 
            {
             ClsCommFuncs.Alert("没有安装Excel程序或者安装错误");
                return;
            }
            app.Workbooks.Add(true);
            app.Visible = false;            Excel.Workbooks books = (Excel.Workbooks)app.Workbooks;
            Excel.Workbook book = (Excel.Workbook)books.Add(Type.Missing);            int iget = 1;
            int dwindex = 0;
            //写头
            if (panelHide1.Visible == true)
            {
                foreach (DataGridViewColumn datcol in dgvLog.Columns)
                {
                    app.Cells[iget, dwindex + 1] = datcol.HeaderText;
                    dwindex++;
                }
            }
            else
            {
                app.Cells[iget, 1] = dgvLog.Columns[0].HeaderText;
                app.Cells[iget, 2] = dgvLog.Columns[1].HeaderText;
                app.Cells[iget, 3] = dgvLog.Columns[2].HeaderText;
                app.Cells[iget, 4] = dgvLog.Columns[5].HeaderText;
            }            //写内容
            int nIndex = 0;
            foreach (TLogData t in m_tLogDataList)
            {
                iget++;                app.Cells[iget, 1] = (++nIndex).ToString();
                app.Cells[iget, 2] = t.GetTime();
                app.Cells[iget, 3] = t.GetLogType();
                if (panelHide1.Visible == true)
                {
                    app.Cells[iget, 4] = t.GetLogCategory();
                    app.Cells[iget, 5] = t.GetUserName();
                    app.Cells[iget, 6] = t.GetLogDecs();
                }
                else
                {
                    app.Cells[iget, 4] = t.GetLogDecs();
                }
            }            app.Cells.EntireColumn.AutoFit();            try
            {
                book.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, 
                    Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, 
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);            }
            catch
            {
                ClsCommFuncs.Alert("导出失败,确认文件没有打开或Excel安装成功");
            }            book.Close(false, Type.Missing, Type.Missing);
            books.Close();
            app.Quit();
            GC.Collect();
        }
其中写内容的这么一段需要不断的写,也就是m_tLogDataList会不断的更新 //写内容
            int nIndex = 0;
            foreach (TLogData t in m_tLogDataList)
            {
                iget++;                app.Cells[iget, 1] = (++nIndex).ToString();
                app.Cells[iget, 2] = t.GetTime();
                app.Cells[iget, 3] = t.GetLogType();
                if (panelHide1.Visible == true)
                {
                    app.Cells[iget, 4] = t.GetLogCategory();
                    app.Cells[iget, 5] = t.GetUserName();
                    app.Cells[iget, 6] = t.GetLogDecs();
                }
                else
                {
                    app.Cells[iget, 4] = t.GetLogDecs();
                }
            }
我尝试将首尾以及写内容的分成三个部分,这样写内容的那段就可以重复使用,可是总是报“因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫”,这样的错误!
不知道怎么解决了,请各位帮忙看看!

解决方案 »

  1.   

    还有,有办法在同一个excel文件尾部写数据么?
      

  2.   

    我是笨方法.
    全部数据变成DataTable.追加完后.再变成Xls
      

  3.   

    每次写完记录下Cells[iget, 4] 
    下次写时 从iget+1开始写
      

  4.   


    m_tLogDataList会更新的,每次写完后,会通知服务器继续发送数据的,收到ACK后m_tLogDataList更新,继续导出,这样讲是不是明白了?
      

  5.   


    服务器一次发送给我15条,我都是保存在m_tLogDataList中的,然后一次性写到excel中。
    你的方法我也想到了,可是每次运行到
    “              app.Cells[iget, 1] = (++nIndex).ToString();”
    就会报错“因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫”
      

  6.   


            Excel.Application m_ExcelApp = null;
            Excel.Workbooks books = null;
            Excel.Workbook book = null;
            private void OnCreateExcelApp()
            {
                if (m_ExportFileName.IndexOf(".") <= 0)
                {
                    m_ExportFileName = m_ExportFileName + ".xls";
                }
                else
                {
                    string[] str = m_ExportFileName.Split('.');
                    if (str.Length > 2)
                    {
                        m_ExportFileName = str[0] + str[str.Length - 1];
                    }
                }            try
                {
                    m_ExcelApp = new Excel.Application();
                }
                catch
                {
                    ClsCommFuncs.Alert("没有安装Excel程序或者安装错误");
                    return;
                }
                m_ExcelApp.Workbooks.Add(true);
                m_ExcelApp.Visible = false;            books = (Excel.Workbooks)m_ExcelApp.Workbooks;
                book = (Excel.Workbook)books.Add(Type.Missing);
                int dwindex = 0;
                //写头
                if (panelHide1.Visible == true)
                {
                    foreach (DataGridViewColumn datcol in dgvLog.Columns)
                    {
                        m_ExcelApp.Cells[1, dwindex + 1] = datcol.HeaderText;
                        dwindex++;
                    }
                }
                else
                {
                    m_ExcelApp.Cells[1, 1] = dgvLog.Columns[0].HeaderText;
                    m_ExcelApp.Cells[1, 2] = dgvLog.Columns[1].HeaderText;
                    m_ExcelApp.Cells[1, 3] = dgvLog.Columns[2].HeaderText;
                    m_ExcelApp.Cells[1, 4] = dgvLog.Columns[5].HeaderText;
                }
            }
            int iget = 1;
            private bool OnExportToExcel()
            {
                //写内容
              
                int nIndex = 0;
                foreach (TLogData t in m_tLogDataList)
                {
                    iget++;                m_ExcelApp.Cells[iget, 1] = (++nIndex).ToString();
                    m_ExcelApp.Cells[iget, 2] = t.GetTime();
                    m_ExcelApp.Cells[iget, 3] = t.GetLogType();
                    if (panelHide1.Visible == true)
                    {
                        m_ExcelApp.Cells[iget, 4] = t.GetLogCategory();
                        m_ExcelApp.Cells[iget, 5] = t.GetUserName();
                        m_ExcelApp.Cells[iget, 6] = t.GetLogDecs();
                    }
                    else
                    {
                        m_ExcelApp.Cells[iget, 4] = t.GetLogDecs();
                    }
                }
                m_ExcelApp.Cells.EntireColumn.AutoFit();
                return true;
            }

            private void OnCloseExcelApp()
            {
                if (book == null||books == null ||m_ExcelApp == null)
                {
                    return;
                }
        
                try
                {
                    book.SaveAs(m_ExportFileName, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                }
                catch
                {
                    ClsCommFuncs.Alert("导出失败,确认文件没有打开或Excel安装成功");
                }            book.Close(false, Type.Missing, Type.Missing);
                books.Close();
                m_ExcelApp.Quit();
                GC.Collect();
            }
    这是我后来改写的代码!