注册 登录
编程论坛 C# 论坛

C#_VS2019求助Microsoft.Office.Interop.Excel库的用法

y3062010247 发布于 2021-04-12 12:13, 1796 次点击
如题Microsoft.Office.Interop.Excel的用法求助
代码如下:
程序代码:
using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Excel;


namespace OPerateExcel
{
    class Program
    {


        static void Main(string[] args)
        {

            string fileName = @"C:\Users\Administrator\Desktop\test.xlsx";
            string saveName = @"C:\Users\Administrator\Desktop\操作.xlsx";
            //set columns
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("订单号", "A");
            dic.Add("数量", "B");
            ApplicationClass excel = new ApplicationClass();//不需要对excel赋值
            Workbook wb = null;//不需要对wb赋值
            excel.Visible = false;
            excel.DisplayAlerts = false;
            wb = excel.Workbooks.Open(fileName);
            Worksheet sht = (Worksheet)wb.Worksheets[1];
            int rowCount = 0;
            rowCount = sht.UsedRange.Rows.Count;
            string orderNum = string.Empty;
            string count = string.Empty;
            //循环行
            for (int i = 0; i < rowCount; i++)
            {
                if (sht.Rows[1] != null)
                {
                    orderNum = sht.Cells[i, dic["订单号"]].ToString();
                    count = sht.Cells[i, dic["数量"]].ToString();
                }
            }
            //循环列
            for (int i = 0; i < sht.UsedRange.Columns.Count; i++)
            {
                //sht.Columns[i];
            }
            wb.Close(wb, saveName);

            Console.ReadKey();
        }
    }
}

问题:
ApplicationClass excel = new ApplicationClass();//不需要对excel赋值
这句报错,注释掉这句前面的代码都可以运行,加上这句程序一开始就进入中断模式,前面的代码也没有被执行,点继续执行程序就直接结束了,请大神帮忙看看是什么问题
3 回复
#2
venomlk2021-04-12 13:23
using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excel;
excel = new Excel.Application();
#3
y30620102472021-04-12 16:37
以下是引用venomlk在2021-4-12 13:23:32的发言:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excel;
excel = new Excel.Application();

看起来好像很有道理,我试一下,非常感谢!
再问一下为什么要这样写,和我上面那样写的有什么区别吗?
#4
y30620102472021-04-12 19:21
以下是引用venomlk在2021-4-12 13:23:32的发言:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excel;
excel = new Excel.Application();

这样写还是不行
程序代码:
using System;
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;


namespace OPerateExcel
{
    class Program
    {


        static void Main(string[] args)
        {

            string fileName = @"C:\Users\Administrator\Desktop\test.xlsx";
            string saveName = @"C:\Users\Administrator\Desktop\操作.xlsx";
            //set columns
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("订单号", "A");
            dic.Add("数量", "B");

            Excel.Application excel;
            excel = new Excel.Application();
            Excel.Workbook wb = null;//不需要对wb赋值
            excel.Visible = true;
            excel.DisplayAlerts = false;
            wb = excel.Workbooks.Open(fileName);
            Excel.Worksheet sht = (Excel.Worksheet)wb.Worksheets[1];
            int rowCount = 0;
            rowCount = sht.UsedRange.Rows.Count;
            string orderNum = string.Empty;
            string count = string.Empty;
            //循环行
            for (int i = 0; i < rowCount; i++)
            {
                if (sht.Rows[1] != null)
                {
                    orderNum = sht.Cells[i, dic["订单号"]].ToString();
                    count = sht.Cells[i, dic["数量"]].ToString();
                }
            }
            //循环列
            for (int i = 0; i < sht.UsedRange.Columns.Count; i++)
            {
                //sht.Columns[i];
            }
            wb.Close(wb, saveName);

            Console.ReadKey();
        }
    }
}

只有本站会员才能查看附件,请 登录
1