zhu2017 2015-07-29 08:53 采纳率: 100%
浏览 2856

C#的dategridview中数据导出到excel的问题

public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
{
MessageBox.Show("请您检查是否有数据导出","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return false;
}
//创建Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);

        //生成字段名称
        for (int i = 0; i < gridView.ColumnCount; i++)
        {
            excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
        }
        //填充数据
        for (int i = 0; i < gridView.RowCount - 1; i++)   //循环行
        {
            for (int j = 0; j < gridView.ColumnCount; j++) //循环列
            {
                if (gridView[j, i].ValueType == typeof(string))
                {
                    excel.Cells[i + 2, j + 1] = "'" + gridView.Rows[i].Cells[j].Value.ToString();
                }
                else
                {
                    excel.Cells[i + 2, j + 1] = gridView.Rows[i].Cells[j].Value.ToString();
                }
            }
        }
        //设置禁止弹出保存和覆盖的询问提示框  
        excel.Visible = false;
        excel.DisplayAlerts = false;
        excel.AlertBeforeOverwriting = false;

        //保存到临时工作簿
        //excel.Application.Workbooks.Add(true).Save();
        //保存文件

        excel.Save("D:" + "\\234.xls");//excel保存界面 点击取消 报错 异常来自 HRESULT:0x800A03EC
        excel.Quit();
        return true;
    }

异常信息:
未处理 System.Runtime.InteropServices.COMException
HResult=-2146827284
Message=异常来自 HRESULT:0x800A03EC
Source=Microsoft.Office.Interop.Excel
ErrorCode=-2146827284
StackTrace:
在 Microsoft.Office.Interop.Excel.ApplicationClass.SaveWorkspace(Object Filename)
在 Login.Form2.ExportDataGridview(DataGridView gridView, Boolean isShowExcle) 位置 D:\BaiduYunDownload\新建文件夹 (3)\新建文件夹\新建文件夹\Login\Form2.cs:行号 404
在 Login.Form2.btn_ex_Click(Object sender, EventArgs e) 位置 D:\BaiduYunDownload\新建文件夹 (3)\新建文件夹\新建文件夹\Login\Form2.cs:行号 355
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.RunDialog(Form form)
在 System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
在 System.Windows.Forms.Form.ShowDialog()
在 Login.Form1.btn_login_Click(Object sender, EventArgs e) 位置 D:\BaiduYunDownload\新建文件夹 (3)\新建文件夹\新建文件夹\Login\Form1.cs:行号 69
在 Login.Form1.txt_password_KeyDown(Object sender, KeyEventArgs e) 位置 D:\BaiduYunDownload\新建文件夹 (3)\新建文件夹\新建文件夹\Login\Form1.cs:行号 109
在 System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
在 System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
在 System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
在 System.Windows.Forms.Control.WmKeyChar(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.TextBoxBase.WndProc(Message& m)
在 System.Windows.Forms.TextBox.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 Login.Program.Main() 位置 D:\BaiduYunDownload\新建文件夹 (3)\新建文件夹\新建文件夹\Login\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:

  • 写回答

1条回答

  • panyuan922 2015-07-29 09:02
    关注

    我一般用NOPI处理EXCEL文件,问题少点

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?