我目前使用:
- Visual Studio 2015 更新 3
- Microsoft Office Professional Plus 2013
- .Net Framework 4.5.1
- Windows 7 64 位
我正在使用以下代码将 Excel 工作表读入 DataTable:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
string filename = "C:\\Users\\myusername\\Documents\\MyFile.xlsx";
DataTable dt = null;
try
{
string ExcelName = filename.Split(("\\").ToCharArray()[0])[filename.Split(("\\").ToCharArray()[0]).Length - 1].Split('.')[0];
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\";";
string SheetName = CommonFunctions.GetExcelSheetNames(ConnectionString)[0];
using (OleDbConnection conn = new OleDbConnection(ConnectionString))
{
string query = string.Format("SELECT * FROM [" + SheetName + "]");
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
if (rdr.HasRows)
{
dt = new DataTable();
dt.TableName = ExcelName;
for (int i = 0; i < rdr.FieldCount; i++)
{
dt.Columns.Add(new DataColumn(rdr.GetName(i), typeof(string)));
}
while (rdr.Read())
{
DataRow dr = dt.NewRow();
for (int i = 0; i < rdr.FieldCount; i++)
{
dr[i] = rdr[i].ToString();
}
dt.Rows.Add(dr);
}
foreach (DataRow row in dt.Rows)
{
string s = row[0].ToString();
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
示例Excel工作表数据如下,以CSV格式提供,方便在Excel中打开:
"store_number", "stock_code", "desired_quantity"
"64004", "BI_KRA_SEL_350065", "1"
"64004", "BI_KRA_SEL_500080", "1"
"86208", "BI_KRA_SEL_350065", "1"
"86208", "BI_KRA_SEL_500080", "1"
"64019", "BI_KRA_SEL_350065", "1"
"64019", "BI_KRA_SEL_500080", "1"
"85858", "BI_KRA_SEL_350065", "1"
"85858", "BI_KRA_SEL_500080", "1"
"72122", "BI_KRA_SEL_350065", "1"
"72122", "BI_KRA_SEL_500080", "1"
"68427", "BI_KRA_SEL_350065", "1"
"68427", "BI_KRA_SEL_500080", "1"
"79031", "BI_KRA_SEL_350065", "1"
"79031", "BI_KRA_SEL_500080", "1"
"67662", "BI_KRA_SEL_350065", "1"
"67662", "BI_KRA_SEL_500080", "1"
"92246", "BI_KRA_SEL_350065", "1"
"92246", "BI_KRA_SEL_500080", "1"
"85432", "BI_KRA_SEL_350065", "1"
"85432", "BI_KRA_SEL_500080", "1"
"87188", "BI_KRA_SEL_350065", "1"
"87188", "BI_KRA_SEL_500080", "1"
"91021", "BI_KRA_SEL_350065", "1"
"91021", "BI_KRA_SEL_500080", "1"
"79022", "BI_KRA_SEL_350065", "1"
"79022", "BI_KRA_SEL_500080", "1"
"86369", "BI_KRA_SEL_350065", "1"
"86369", "BI_KRA_SEL_500080", "1"
"67670", "BI_KRA_SEL_350065", "1"
"67670", "BI_KRA_SEL_500080", "1"
"92605", "BI_KRA_SEL_350065", "1"
"92605", "BI_KRA_SEL_500080", "1"
"92609", "BI_KRA_SEL_350065", "1"
"92609", "BI_KRA_SEL_500080", "1"
"92610", "BI_KRA_SEL_350065", "1"
"92610", "BI_KRA_SEL_500080", "1"
"92611", "BI_KRA_SEL_350065", "1"
"92611", "BI_KRA_SEL_500080", "1"
"92612", "BI_KRA_SEL_350065", "1"
"92612", "BI_KRA_SEL_500080", "1"
"92613", "BI_KRA_SEL_350065", "1"
"92613", "BI_KRA_SEL_500080", "1"
"92614", "BI_KRA_SEL_350065", "1"
"92614", "BI_KRA_SEL_500080", "1"
"92615", "BI_KRA_SEL_350065", "1"
"92615", "BI_KRA_SEL_500080", "1"
"92616", "BI_KRA_SEL_350065", "1"
"92616", "BI_KRA_SEL_500080", "1"
"w090", "BI_KRA_SEL_350065", "1"
"w090", "BI_KRA_SEL_500080", "1"
"C908", "BI_KRA_SEL_350065", "1"
"C908", "BI_KRA_SEL_500080", "1"
"w0901", "BI_KRA_SEL_350065", "1"
"w0901", "BI_KRA_SEL_500080", "1"
"G202", "BI_KRA_SEL_350065", "1"
"G202", "BI_KRA_SEL_500080", "1"
问题是当第一列包含字母和/或空格时。这些单元格在生成的数据表 (dt) 中显示为空白,即从“w090”到“G202”。
我发现当单元格格式设置为“常规”时会发生这种情况。但是,将这些单元格的格式更改为“文本”似乎可以解决问题。
我现在遇到的唯一问题是我不能指望我的客户提供将单元格设置为“文本”格式的文件。
有没有人知道这个问题的修复方法,或者是否有可能克隆具有“文本”格式的 Excel 文件的方法?
也许有人知道将 Excel 文件导入 DataTables/DataSets 的更聪明的方法。
非常感谢任何帮助。
最佳答案
由于您将 Excel
用作数据库,因此每个字段(列)都必须具有其确切的数据类型。 Excel
数据库驱动程序根据第一个值猜测此类型。在您的情况下,这些第一个值在第一列中是数字。所以数据库在那里猜测数字数据类型。因此,后来出现的字符串不适合该类型。
数据库驱动程序有一个参数IMEX
,它导致将所有 数据视为文本。参见 https://www.connectionstrings.com/ace-oledb-12-0/treating-data-as-text/ .
那么试试
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;IMEX=1\";";
或
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\";";
关于c# - 通过 C# 从 Excel 文件导入通用格式的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41381815/