如何在为Excel表格添加列时避免使用数组? [英] How can I avoid Array while adding columns for Excelsheet?

查看:65
本文介绍了如何在为Excel表格添加列时避免使用数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,

我在我的项目中将文件作为.csv上传,在流阅读器的帮助下,我将excel列添加到我的sql表中..



Hii,
I am uploading file as .csv in my project and with the help of stream reader i am taking excel columns to my sql table ..

string[] rec = line.Split(',');
                                if (rec.Length > 0)
                                {
                                    DataRow dr = myExcelTable.NewRow();
                                    if (rec[0] != null)
                                    {
                                        dr["EmployeeId"] = rec[0];
                                    }
                                    else
                                    {
                                        dr["EmployeeId"] = "";
                                    }

                                    if (rec[1] != null)
                                    {
                                        dr["Date"] = rec[1];
                                    }
                                    else
                                    {
                                        dr["Date"] = "";
                                    }







这就是我在做什么..但是如果我的excel已经选择了列...如果我的excel有很多列怎么办...你能建议我替换数组这里我用于Datarow和String [] rec这里




this is what i am doing .. bt this will be ok if my excel has selected column ... what if my excel has many column .. can you suggest me alternative for array here which i used for Datarow and String[] rec here

推荐答案

不要使用Split。

您可以在此搜索CSV阅读器或尝试 Rive [ ^ ]。



您是否尝试将CS​​V与现有Excel表格匹配?或者只是导入CSV?

如果是前者,我建议导入CSV然后将数据复制到工作表。
Don't use Split.
You can search here for CSV readers or try Rive[^].

Are you trying to match the CSV to an existing Excel sheet? Or simply import the CSV?
If the former, I recommend importing the CSV and then copying the data to the sheet.


试试这个,它会返回一个数据表,您可以从数据表中获取所需的所有列



TRy this, it will return a data table , from data table you can get all column as required

public DataTable SelectExcelSheet(string strSheetName, string strSheetRange)
        {
            try
            {
                //Open and query
                if (oleConn == null) Open();

                if (oleConn.State != ConnectionState.Open)
                    return null;

                DataTable dataTable = new DataTable(strSheetName);

                //Fill table
                string SelectQuery = string.Format("SELECT * FROM [{0}


{1}],strSheetName,strSheetRange);
使用(OleDbDataAdapter oleAdapter = new OleDbDataAdapter(SelectQuery,oleConn))
{
oleAdapter.FillSchema(dataTable,SchemaType.Source);
oleAdapter.Fill(dataTable);
}
dataTable.Dispose();
return dataTable;
}
catch (例外情况)
{
throw ex;
}
最后
{
if (oleConn != null )Close();
}
}
{1}]", strSheetName, strSheetRange); using (OleDbDataAdapter oleAdapter = new OleDbDataAdapter(SelectQuery, oleConn)) { oleAdapter.FillSchema(dataTable, SchemaType.Source); oleAdapter.Fill(dataTable); } dataTable.Dispose(); return dataTable; } catch (Exception ex) { throw ex; } finally { if (oleConn != null) Close(); } }


这篇关于如何在为Excel表格添加列时避免使用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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