zhu2017 2015-07-29 00:53 采纳率: 100%
浏览 2861

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 01:02
    关注

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

    评论
编辑
预览

报告相同问题?