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文件,问题少点

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R