如何在c#中将不同的参数传递给同一个方法 [英] how to pass different arguments to same method in c#

查看:82
本文介绍了如何在c#中将不同的参数传递给同一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用以下代码从excel表中提取数据并将其保存到另一个excel文件。



我的任务是从另一个excel文件中提取相同内容并比较两个数据以检查常见数据



Hi,

I am using the following code to extract data from excel sheet and save it to another excel file.

My task is to extract the same from another excel file and compare both the data to check for common data

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Collections;

namespace ConsoleApplication31
{
    class Program
    {
        
        private DataSet GetExcelData(string ExcelPath, string ExcelFileName)
        {
            object misValue = System.Reflection.Missing.Value;
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;


            string Provider = string.Empty;
            string ExtendedProperties = string.Empty;
            List<string> listSheetNames = new List<string>();
            DataSet dataSet = new DataSet();
            OleDbConnection connection = null;
            try
            {
                // for 97-03 Excel file
                if (Path.GetExtension(ExcelFileName).Equals(".xls"))
                {
                    Provider = "Microsoft.Jet.OLEDB.4.0";
                    ExtendedProperties = "Excel 8.0;HDR=Yes;IMEX=1";
                }
                // for 2007 Excel file
                else if (Path.GetExtension(ExcelFileName).Equals(".xlsx"))
                {
                    Provider = ".xlsx";
                    ExtendedProperties = "Excel 12.0;HDR=Yes;IMEX=1";
                }
                try
                {
                    connection = new OleDbConnection("provider =" + Provider + "; Data Source='" + ExcelPath + ExcelFileName + "';" + "Extended Properties=\"" + ExtendedProperties + "\";");
                    connection.Open();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error in opening the file " + ExcelFileName + "\nDetails: " + ex.Message);
                }

                listSheetNames = GetExcelSheetName(connection);
                OleDbDataAdapter command = new OleDbDataAdapter("Select * From [" + listSheetNames[0] + "]", connection);
                command.Fill(dataSet);
                connection.Close();

                
                string path1 = @"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log file data\Output";
                // Print the value of columns 1 in each row 
                if (File.Exists(path1))
                    File.Delete(path1);
                int i = 1;

                
                foreach (DataRow row in dataSet.Tables[0].Rows)
                {
                    
                    xlWorkSheet.Cells[i, 1] = row[1];
                    //msgID = row[1];
                    
                    i++;
                }
               
                xlWorkBook.SaveAs(path1, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(path1);
                xlApp.Visible = true;
                
                    
                
                
                
            }
            catch (Exception ex)
            {
                throw new Exception("Error in retrieving data from the file " + ExcelFileName + "\nDetails: " + ex.Message);
            }
            return dataSet;
        }

        private List<string> GetExcelSheetName(OleDbConnection connection)
        {
            List<string> listSheetNames = new List<string>();
            try
            {
                DataTable dtSheet = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                foreach (DataRow drSheet in dtSheet.Rows)
                {
                    if (drSheet["TABLE_NAME"].ToString().Contains("$"))//checks whether row contains '_xlnm#_FilterDatabase' or sheet name(i.e. sheet name always ends with $ sign)
                    {
                        listSheetNames.Add(drSheet["TABLE_NAME"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Could not read Excel sheet names\nDetails: " + ex.Message);
            }
            return listSheetNames;
        }

 static void Main()
        {
            var p = new Program();
            p.Bar();
        }

        void Bar()
        {


            GetExcelData(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\Output\", "MsgYellow.xls");
           
            

        }
    }
}





任何人都可以建议我,我该怎么办呢

谢谢

John



[edit]已添加代码块 - OriginalGriff [/ edit]



Can anyone suggest me , how can I do this
Thanks
John

[edit]Code block added - OriginalGriff[/edit]

推荐答案

)) // 检查行是否包含'_xlnm#_FilterDatabase'或工作表名称(即工作表名称始终以
"))//checks whether row contains '_xlnm#_FilterDatabase' or sheet name(i.e. sheet name always ends with


符号结尾)
{
listSheetNames.Add(drSheet [ TABLE_NAME]。ToString());
}
}
}
catch (例外情况)
{
throw new 异常( 无法读取Excel工作表名称\\\
Details:
+ ex.Message);
}
return listSheetNames;
}

静态 void Main()
{
var p = new Program();
p.Bar();
}

void Bar()
{


GetExcelData( @ \\global.scd.scania.com\home\\\\121\valhbc \\ \\Desktop\log files \Output \ MsgYellow.xls );



}
}
}
sign) { listSheetNames.Add(drSheet["TABLE_NAME"].ToString()); } } } catch (Exception ex) { throw new Exception("Could not read Excel sheet names\nDetails: " + ex.Message); } return listSheetNames; } static void Main() { var p = new Program(); p.Bar(); } void Bar() { GetExcelData(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\Output\", "MsgYellow.xls"); } } }





任何人都可以建议我,我该怎么办呢

谢谢

John



[edit]已添加代码块 - OriginalGriff [/ edit]



Can anyone suggest me , how can I do this
Thanks
John

[edit]Code block added - OriginalGriff[/edit]


嗯,你可以先使用你提取的数据:

Well, you could start by using the data you fetched:
void Bar()
{
    GetExcelData(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\Output\", "MsgYellow.xls");
}

获取数据,然后立即将其抛弃。

您可以尝试保留它,并获取第二组数据吗?

Gets the data, then throws it away immediately.
You could try keeping it, and fetching a second set as well?

void Bar()
{
    DataSet ds1 = GetExcelData(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\Output\", "MsgYellow.xls");
    DateSet ds2 = GetExcelData(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\Output\", "OtherFile.xls");
}





但说实话,看起来你已经从互联网上随机抓取了一种方法,而不用担心太多它实际上做了什么并将其用于你的代码...我怀疑你不需要做一半的实际完成你的作业。



But to be honest, it looks like you have grabbed a method randomly from the internet without worrying too much about what it actually does and bolted it into your code...I suspect that you don't need to do half of what that does to actually finish your homework.


这篇关于如何在c#中将不同的参数传递给同一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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