【发布时间】:2017-05-13 21:41:04
【问题描述】:
我目前使用:
- 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"
问题是当第一列包含字母和/或空格时。这些单元格在生成的 DataTable (dt) 中显示为空白,即从“w090”到“G202”。
我发现当单元格格式设置为“常规”时会发生这种情况。但是,将这些单元格的格式更改为“文本”似乎可以解决问题。
我现在唯一的问题是我不能依赖我的客户提供将单元格设置为“文本”格式的文件。
有没有人知道解决这个问题的方法,或者可能是克隆具有“文本”格式的 Excel 文件的方法?
也许有人知道将 Excel 文件导入 DataTables/DataSets 的更聪明的方法。
非常感谢任何帮助。
【问题讨论】:
-
不知道为什么
string filename = "C:\Users\myusername\Documents\MyFile.xlsx";应该工作。不是必须是string filename = @"C:\Users\myusername\Documents\MyFile.xlsx";或string filename = "C:\\Users\\myusername\\Documents\\MyFile.xlsx";吗?或者是否有一些通用设置将所有字符串文字都作为逐字字符串文字? -
@Axel Richter 谢谢。我已经更正了字符串。注意:我可以选择在字符串前面加上 @ 符号。
-
Hambone 的回答促使我将
adapter.fill替换为DataReader,以便更好地控制调试。在当前调查点,发现数据在rdr.Read()之前或之前丢失。
标签: c# .net excel import datatable