【问题标题】:How to choose a value from cell in ExcelExcel中如何从单元格中选择一个值
【发布时间】:2014-03-11 07:54:55
【问题描述】:

如何从 Excel 中的单元格中正确选择值?我知道我的问题在于选择单元格的命令。

我的实际脚本:

List<string> wsList = new List<string>();
DataTable schemaTable;
DataSet da = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
string name;
string FileName = fullpath;
string _ConnectionString = string.Empty;
string _Extension = Path.GetExtension(FileName);
// Checking for the extentions, if XLS connect using Jet OleDB
if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
{
    _ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0", FileName);
}
// Use ACE OleDb
else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
    _ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", FileName);
}

OleDbConnection con = new OleDbConnection(_ConnectionString);

try
{
    con.Open();
    // Get schematable name from excel file
    schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    foreach (DataRow row in schemaTable.Rows)
        wsList.Add(row.Field<string>("TABLE_NAME"));
    name = wsList[0];
    // Select values from cell in excel
    string strCmd = "SELECT J38 FROM [" + name + "]"; // I think that here is my main problem
    // Command for select value
    OleDbCommand cmd = new OleDbCommand(strCmd, con);

    da.Clear();
    adapter.SelectCommand = cmd;
    adapter.Fill(da);
    UniqueValue.money.Add(double.Parse(da.ToString()));
}

catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

finally
{
    con.Close();
}

您可以在其中观看我需要的内容的图片选择:(合并单元格)

调试:

对于一个或多个必需的参数没有找到,没有值

【问题讨论】:

  • 对我的问题有什么帮助吗?

标签: c# excel merge command cells


【解决方案1】:

合并的单元格和数据适配器不会混合使用。如果这是一次性的,请将原始工作表中的值复制到一个空工作簿中并取消合并单元格,手动进行任何其他清理。如果这是一个需要重复的生产过程,并且量很大,请考虑在工作簿中编写一个 VBA 宏来为您执行复制粘贴/清理过程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    相关资源
    最近更新 更多