使用C Sharp操纵Excel Spreadsheats [英] Manipulating Excel Spreadsheats using C sharp

查看:49
本文介绍了使用C Sharp操纵Excel Spreadsheats的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C Sharp .net 2010中操作Excel Spreadsheets.我要做的总和是循环遍历excel电子表格,根据其他电子表格的一些计算将三列添加到原始列的左侧加上其他一些功能.最新和最好的方法是什么?目前,我正在打开excel电子表格并阅读所需的记录,并成功将其打印到屏幕上:

I need to manipulate Excel Spreadsheets in C sharp .net 2010. The sum total of what I need to do is loop through an excel spreadsheet, add three columns to the left of the original columns based on some calculations from the other spreadsheets plus some other functions. What is the latest and greatest way to do this? Currently I am opening the excel spreadsheet and reading the records I need and printing them to screen with this function succefully:

    public static void parseData(string excelFileName)
    {
        try
        {
            //Workbook workBook = _excelApp.Workbooks.Open(excelFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //Worksheet wkSheet = (Worksheet)_excelApp.Worksheets[1];
            //OLEObject oleObject = wkSheet.OLEObjects(Type.Missing);
            using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\""))
            {
                connection.Open();
                OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

                objAdapter1.SelectCommand = objCmdSelect;
                objAdapter1.Fill(ds);
            }

            foreach (System.Data.DataTable myTable in ds.Tables)
            {
                //xmlResultSet.Append("<TemplateData>");                                        
                foreach (DataRow myRow in myTable.Rows)
                {
                    //Console.WriteLine("Ca0mon, please work :{0}", myRow);
                    Console.WriteLine("This thing better Work-- MRN: {0}| Patient Name: {1}| Address: {2}| CSSV: {3}", myRow[0], myRow[1], myRow[2], myRow[3]);
                }
            }

        }
        catch(Exception ex)
        {
        }
    }

关于如何使用最新,最出色的.net工具解决此问题的任何建议?感谢您的帮助.

Any advice on how to approach this with the latest and greatest .net Tools? Thanks for your help.

推荐答案

在安装Office时,请选择安装Interop库.完成此操作后,转到添加引用,单击 COM 选项卡,然后添加 Microsoft Excel 12.0对象库.

With your Office installation, opt to install the Interop libraries. After you have done this, go to Add Reference, click on the COM tab, and add Microsoft Excel 12.0 Objects library.

使用Microsoft.Office.Interop.Excel;

Use the namespace using Microsoft.Office.Interop.Excel;

然后,您应该阅读一个教程,例如 MSDN上的"".

You should then go through a tutorial such as this one on MSDN.

这篇关于使用C Sharp操纵Excel Spreadsheats的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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