我正在使用以下代码读取Excel并将其存储在数据集中。
public DataSet ReadExcelDataToDataSet(Stream fileStream)
{
DataTable dataInExcelSheet = new DataTable();
IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(fileStream);
DataSet excelDataSet = excelReader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = false
//Do we have any property here to convert all rows values to string datatype.
});
excelReader.Close();
return excelDataSet;
}
是否可以将Excel Sheet的所有值转换为字符串并将其作为字符串值存储在数据集中。
示例:
在Excel文件中,对于少数几列,我具有Int32和Double数据类型的值1,22.0。我想将这些值转换为字符串,然后将它们作为字符串存储在数据集中。
答案 0 :(得分:1)
使用UseColumnDataType = false
时,ExcelDataReader返回单元格为System.Object
。
如果您需要退回System.String
,则需要克隆DataSet
结构,并在使用时将每个DataColumn
的类型设置为System.String
,然后使用DataTable.ImportRow()
为您完成所有向System.String
的转换。如果您需要进一步控制格式设置(例如,转换日期类型),也可以显式进行转换。
这是给你的例子...
// NuGet reference: ExcelDataReader v3.6.0 built from https://github.com/ExcelDataReader/ExcelDataReader/ExcelDataReader
// NuGet reference: ExcelDataReader.DataSet v3.6.0 built from https://github.com/ExcelDataReader/ExcelDataReader/ExcelDataReader.DataSet
using ExcelDataReader;
using System;
using System.Data;
using System.IO;
using System.Linq;
namespace Convert_IExcelDataReader_values_to_string
{
class MainClass
{
public static DataSet ReadExcelDataToDataSet(Stream fileStream)
{
DataSet excelDataSet;
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(fileStream))
{
var dataSetConfiguration = new ExcelDataSetConfiguration()
{
UseColumnDataType = false
};
// This reads each Sheet into a DataTable and each column is of type System.Object
excelDataSet = reader.AsDataSet(dataSetConfiguration);
}
var stringDataSet = ConvertToDataSetOfStrings(excelDataSet);
return stringDataSet;
}
private static DataSet ConvertToDataSetOfStrings(DataSet sourceDataSet)
{
var result = new DataSet();
result.Tables.AddRange(
sourceDataSet.Tables.Cast<DataTable>().Select(srcDataTable =>
{
var destDataTable = new DataTable(srcDataTable.TableName, srcDataTable.Namespace);
// Copy each source column as System.String...
destDataTable.Columns.AddRange(
srcDataTable.Columns.Cast<DataColumn>()
.Select(col => new DataColumn(col.ColumnName, typeof(String)))
.ToArray()
);
// Implicitly convert all source cells to System.String using DataTable.ImportRow()
srcDataTable.Rows.OfType<DataRow>()
.ToList()
.ForEach(row => destDataTable.ImportRow(row));
return destDataTable;
})
.ToArray()
);
return result;
}
public static void Main(string[] args)
{
using (var stream = File.Open("TestOpenXml.xlsx", FileMode.Open, FileAccess.Read, FileShare.Read))
{
var dataSet = ReadExcelDataToDataSet(stream);
}
}
}
}
希望这会有所帮助!
答案 1 :(得分:1)
使用扩展名的非LINQ
版本添加到@AlwaysLearning答案。
public static class DataSetExtensions
{
public static DataSet ToAllStringFields(this DataSet ds)
{
// Clone function -> does not copy the data, but just the structure.
var newDs = ds.Clone();
foreach (DataTable table in newDs.Tables)
{
// if the column is not string type -> set as string.
foreach (DataColumn col in table.Columns)
{
if (col.DataType != typeof(string))
col.DataType = typeof(string);
}
}
// imports all rows.
foreach (DataTable table in ds.Tables)
{
var targetTable = newDs.Tables[table.TableName];
foreach (DataRow row in table.Rows)
{
targetTable.ImportRow(row);
}
}
return newDs;
}
}
用法:
public DataSet ReadExcelDataToDataSet(Stream fileStream)
{
DataTable dataInExcelSheet = new DataTable();
IExcelDataReader excelReader = ExcelReaderFactory.CreateReader(fileStream);
DataSet excelDataSet = excelReader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = false
}).ToAllStringFields();
excelReader.Close();
return excelDataSet;
}