命名空间“Microsoft.Office.Interop”中不存在类型或命名空间名称“Excel”

老齐 posted @ 2009年5月26日 23:40 in Winform , 14712 阅读

c#中引用excel组建导出excel,换了台机器竟然提示“Microsoft.Office.Interop”中不存在类型或命名空间名称“Excel””

处理了一下:

方法1:重新安装Office,在OFFICE的安装详细列表里面EXCEL中有一个.NET程序支持的选项。

方法2:
1. 在visual Studio 2005命令提示工具中,定位到Excel安装目录,运行“TlbImp EXCEL.EXE”命令,把Excel.exe编译为Excel.dll,
3. 引用刚编译好的Excel.dll,添加引用using Excel;删除Microsoft.Office.Interop.Excel;
用Excel替换Microsoft.Office.Interop.Excel。

using Excel;
//using Application=Microsoft.Office.Interop.Excel.Application;
using Application = Excel.Application;

导出excel的代码:

 

private void btDC_Click(object sender, EventArgs e) //导出到excel
        {
            if (
                MessageBox.Show("是否将当前页面日志信息导出到excel中?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) ==
                DialogResult.OK)
            {
                try
                {
                    goexceldt = new goececlDT();
                    DataTable dtappen;
                    if (tabControl1.SelectedIndex == 0)
                    {
                        dtappen = goexceldt.GetDatatablerzyb(); //调用SQL语句执行查询(异步的日志)
                    }
                    else
                    {
                        if (cmblyk.Text == null || cmblyk.Text == "")
                        {
                            MessageBox.Show("请选择来源库", "提示");
                            return;
                        }
                        dtappen = goexceldt.GetDatatablerztb(layer); //调用SQL语句执行查询(同步的日志)
                    }

                    //实例化一个Excel.Application对象   
                    Application excel = new Application();
                    //让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写   
                    excel.Visible = false;
                    Workbooks ibooks = excel.Workbooks; //获得工作薄的集合
                    Workbook book = ibooks.Add(Missing.Value); //添加一个工作薄采用缺省参数
                    Worksheet sheet1 = (Worksheet) book.Worksheets[1]; //获得第一个SHEET页
                    if (tabControl1.SelectedIndex == 0)
                    {
                        sheet1.Cells[1, 1] = "任务名称";
                        sheet1.Cells[1, 2] = "目标库名称";
                        sheet1.Cells[1, 3] = "迁移日期";
                        sheet1.Cells[1, 4] = "来源库名称";
                        sheet1.Cells[1, 5] = "目标表名称";
                        sheet1.Cells[1, 6] = "开始时间";
                        sheet1.Cells[1, 7] = "结束时间";
                        sheet1.Cells[1, 8] = "来源表录入时间戳";
                        sheet1.Cells[1, 9] = "迁移记录数";
                        sheet1.Cells[1, 10] = "更新记录数";
                        sheet1.Cells[1, 11] = "出错记录数";
                        sheet1.Cells[1, 12]"错误信息";
                        //sheet1.Cells[1, 12] = "出错记录数";
                        //sheet1.Cells[1, 6] = "迁移批次";
                        //sheet1.Cells[1, 7] = "开始时间";
                        //sheet1.Cells[1, 8] = "结束时间";
                        //sheet1.Cells[1, 9] = "来源表录入时间戳";
                        //sheet1.Cells[1, 10] = "迁移记录数";
                        //sheet1.Cells[1, 11] = "更新记录数";
                        //sheet1.Cells[1, 12] = "出错记录数";
                        int count = 1;
                        foreach (DataRow dr in dtappen.Rows)
                        {
                            sheet1.Cells[count + 1, 1] = dr["RWMC"].ToString();
                            sheet1.Cells[count + 1, 2] = dr["ytkmc"].ToString();
                            sheet1.Cells[count + 1, 3] = dr["QYRQ"].ToString();
                            sheet1.Cells[count + 1, 4] = dr["ytkmc1"].ToString();
                            sheet1.Cells[count + 1, 5] = dr["bmc"].ToString();
                            sheet1.Cells[count + 1, 6] = dr["qykssj"].ToString();
                            sheet1.Cells[count + 1, 7] = dr["qyjssj"].ToString();
                            sheet1.Cells[count + 1, 8] = dr["lrsj"].ToString();
                            sheet1.Cells[count + 1, 9] = dr["qyjls"].ToString();
                            sheet1.Cells[count + 1, 10] = dr["gxjls"].ToString();
                            sheet1.Cells[count + 1, 11] = dr["qyztbz"].ToString();
                            sheet1.Cells[count + 1, 12] = dr["cwxx"].ToString();

                            count += 1; // 计数器累加<行数累加>
                        }
                    }
                    else
                    {
                        sheet1.Cells[1, 1] = "日志ID";
                        sheet1.Cells[1, 2] = "标准表PK";
                        sheet1.Cells[1, 3] = "迁移日期";
                        sheet1.Cells[1, 4] = "迁移开始时间";
                        sheet1.Cells[1, 5] = "迁移结束时间";
                        sheet1.Cells[1, 6] = "新增记录数";
                        sheet1.Cells[1, 7] = "更新记录数";
                        sheet1.Cells[1, 8] = "删除记录数";
                        sheet1.Cells[1, 9] = "错误记录数";
                        sheet1.Cells[1, 10] = "数据备份文件名";
                        int count = 1;
                        foreach (DataRow dr in dtappen.Rows)
                        {
                            sheet1.Cells[count + 1, 1] = dr["RZ_ID"].ToString();
                            sheet1.Cells[count + 1, 2] = dr["MBB_ID"].ToString();
                            sheet1.Cells[count + 1, 3] = dr["QYRQ"].ToString();
                            sheet1.Cells[count + 1, 4] = dr["QYKSSJ"].ToString();
                            sheet1.Cells[count + 1, 5] = dr["QYJSSJ"].ToString();
                            sheet1.Cells[count + 1, 6] = dr["XZJLS"].ToString();
                            sheet1.Cells[count + 1, 7] = dr["GXJLS"].ToString();
                            sheet1.Cells[count + 1, 8] = dr["SJJLS"].ToString();
                            sheet1.Cells[count + 1, 9] = dr["CWJLS"].ToString();
                            sheet1.Cells[count + 1, 10] = dr["BFWJM"].ToString();
                            count += 1; // 计数器累加<行数累加>
                        }
                    }

                    excel.DisplayAlerts = false;
                    excel.AlertBeforeOverwriting = true;
                    string excelFileName = string.Empty;
                    SaveFileDialog sf = new SaveFileDialog();
                    String a = "异步迁移任务日志";
                    if (tabControl1.SelectedIndex == 0)
                    {
                        a = "异步迁移任务日志";
                    }
                    else
                    {
                        a = cmblyk.SelectedText + "源头库的同步迁移任务日志";
                    }
                    if (sf.FileName.Length == 0)
                    {
                        string datenow = DateTime.Now.ToShortDateString();
                        sf.FileName = a + datenow;
                    }

                    sf.Filter = "*.xls|*.*";
                    if (sf.ShowDialog() == DialogResult.OK)
                    {
                        excelFileName = sf.FileName;
                    }
                    else
                    {
                        return;
                    }

                    book.SaveAs(excelFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                XlSaveAsAccessMode.xlNoChange,
                                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    excel.Quit();
                    excel = null;
                    MessageBox.Show("导出Excel成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                    return;
                }
            }
        }
 

 

Avatar_small
maid services dubai 说:
2019年10月24日 21:10

You’re able to clean all house for about nearly four hours in one week. I perfected this pictures had my student's residential house-cleaning home business. One justification that cleaning other's houses is easier than cleaning your current, is actually are not involved in the mess. Nothing is personal while you don't become distracted as long as you're cleaning.

Avatar_small
sikkimhealth.org 说:
2020年4月20日 18:32

The mind and body are employed to doing precisely what they often do---in various other words, homeostasis as well as equilibrium. They may maintain claims of wellbeing, but additionally, they maintain claims of uneven health. To acquire healthy, your inside teacher has got to push anyone beyond pleasantly unhealthy practices. On the opposite hand, your inside teacher boasts to patiently enable you to through these kind of potentially cumbersome transition times and often remind anyone: "I are able to do this! "

Avatar_small
cleantechlaws.com 说:
2020年4月20日 18:33

On-line law tuition has grown an raising popular selection both pertaining to students along with law tutors. Indeed a lot of tutors now want to teach his or her students by way of Skype and also other online solutions. Some anticipate that with the year 2017 nearly all LLB tutors are going to be partaking throughout online legislations tuition around traditional localized private tutoring.

Avatar_small
bankruptcylawmerced. 说:
2020年4月20日 18:33

The phrase civil legislations derives through the Latin expression ius civile, legislation applicable to everyone Roman individuals. This ended up being established inside sixteenth one hundred year. Europe was the 1st to take up this legislations and the majority Europe coached this in any respect their schools.

Avatar_small
duilawyermontreal.co 说:
2020年4月20日 18:33

Legislation of Destination, otherwise named and termed as Circumstances and outcomes or Sowing and Acquiring is, comparable to all Prevalent Laws, amazingly required to comprehend along with deliberately execute for the off chance you are to discover how to intentionally along with deliberately bring in the longings of your respective heart into your health.

Avatar_small
wendywoodlaw.com 说:
2020年4月20日 18:34

This can be a law that will states many of us have your potential within just us to make great issues. The exercises to generate this law be right for you include introspection, listening for you to nature, and just being tranquil for very long stretches during a period to realize our inside self plus the messages within just.

Avatar_small
full time maids in d 说:
2020年5月05日 19:40

Taking into consideration down to help writing ones maid connected with honor dialog, sometimes it truly is hard to recognise how to start! We delay, we get burned out about it then delay a very little longer. Tend not to worry, focus mind and attempt to start publishing your dialog now! Just abide by the ways below in addition to write the nice maid connected with honor speeches and toasts, I learn you many can offer.

Avatar_small
painting company in 说:
2020年5月05日 19:40

Its also wise to feel that your particular painter is aware of what they are doing. You could possibly enjoy without having to placed yourself at an increased risk by balancing with a large spectrum, but should you be constantly worried that this professional that you just hired could fall and have hurt, it makes the task stressful.

Avatar_small
maids in dubai 说:
2021年6月08日 16:34

Let’s make anything very clean that disinfection would definitely only work that the specific supports are tidy. So, invest your efforts in vacuuming, first. Therefore, you can implement the disinfection approach. Whether you will find anyone frustrated or not on your property, you will need to regularly tidy highly-touched supports. Because germs can stay for long periods on very hard surfaces.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter