将数据从DB获取到Excel [英] Getting Data from DB to Excel

查看:100
本文介绍了将数据从DB获取到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从本地系统中的数据库获取值到Excel工作表。这是我的代码



 private void button1_Click(object sender,EventArgs e)
{
Microsoft.Office.Interop .Excel.Application Text_To_Excel = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbooks Excel_File =(Microsoft.Office.Interop.Excel.Workbooks)Text_To_Excel.Workbooks;

Excel_File.OpenDatabase(@C:\Program Files \ Microsoft SQL Server \ MSSQL11.MSSQLSERVER \ MSSQL \DATA \ SportsStore.mdf);

Text_To_Excel.ActiveWorkbook.SaveAs(location +Mq.xlsx,Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook,System.Reflection.Missing.Value,System.Reflection.Missing.Value, System.Reflection.Missing.Value,System.Reflection.Missing.Value,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing。 Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value);

Text_To_Excel.ActiveWorkbook.Close(false,System.Reflection.Missing.Value,System.Reflection.Missing.Value);
}







当我运行它时,我得到一个COMException未处理错误

来自HRESULT的异常:0x800A03EC 

错误代码

 -2146827284 





我在这里做错了什么以及如何解决?

解决方案

通过本教程: [<如何将SQL数据导出到excel中a href =http://dotnetmentors.com/c-sharp/how-to-export-sql-data-to-excel-using-microsoft-office-interop.aspxtarget =_ blanktitle =New Window > ^ ]

I am trying to get values from a DB in my local system to Excel Sheet. This is my code

private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application Text_To_Excel = new Microsoft.Office.Interop.Excel.Application();

            Microsoft.Office.Interop.Excel.Workbooks Excel_File = (Microsoft.Office.Interop.Excel.Workbooks)Text_To_Excel.Workbooks; 
           
            Excel_File.OpenDatabase(@"C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\SportsStore.mdf");

            Text_To_Excel.ActiveWorkbook.SaveAs(location + "Mq.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);

            Text_To_Excel.ActiveWorkbook.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
        }




When I run it, I get a COMException was unhandled error

Exception from HRESULT: 0x800A03EC

with error code

-2146827284



What am I doing wrong here and how do I resolve it ?

解决方案

Go thru this tutorial: How to export SQL data to excel using Microsoft.Interop.Office.Excel and C# [^]


这篇关于将数据从DB获取到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆