| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1592 人关注过本帖
标题:C#_VS2019求助Microsoft.Office.Interop.Excel库的用法
只看楼主 加入收藏
y3062010247
Rank: 1
来 自:甘肃
等 级:新手上路
帖 子:8
专家分:0
注 册:2021-1-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
C#_VS2019求助Microsoft.Office.Interop.Excel库的用法
如题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赋值
这句报错,注释掉这句前面的代码都可以运行,加上这句程序一开始就进入中断模式,前面的代码也没有被执行,点继续执行程序就直接结束了,请大神帮忙看看是什么问题
搜索更多相关主题的帖子: Office 用法 Microsoft Excel string 
2021-04-12 12:13
venomlk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:15
专家分:85
注 册:2009-2-18
收藏
得分:20 
using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excel;
excel = new Excel.Application();
2021-04-12 13:23
y3062010247
Rank: 1
来 自:甘肃
等 级:新手上路
帖 子:8
专家分:0
注 册:2021-1-3
收藏
得分:0 
以下是引用venomlk在2021-4-12 13:23:32的发言:

using Excel = Microsoft.Office.Interop.Excel;

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

看起来好像很有道理,我试一下,非常感谢!
再问一下为什么要这样写,和我上面那样写的有什么区别吗?
2021-04-12 16:37
y3062010247
Rank: 1
来 自:甘肃
等 级:新手上路
帖 子:8
专家分:0
注 册:2021-1-3
收藏
得分:0 
以下是引用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();
        }
    }
}

图片附件: 游客没有浏览图片的权限,请 登录注册
2021-04-12 19:21
快速回复:C#_VS2019求助Microsoft.Office.Interop.Excel库的用法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026660 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved