我刚刚开始处理类似的情况,但我相信这可能会满足您的需求:
http://www.codeproject.com/Articles/246772/Convert-xlsx-xls-to-csv
这使用您可以从 NuGet 获得的 ExcelDataReader api
http://exceldatareader.codeplex.com/
正如 Tim 所说,您将必须确保并可能验证工作表之间的列和结构是否相同。您可能还必须在第一张之后吃掉所有床单上的标题行。完成后,我将发布更新和一些代码示例。
更新 [7/15/2013]。这是我完成的代码。不是很花哨,但它可以完成工作。所有工作表都是 DataSet 中的表,因此您只需遍历添加到目标的表。我正在输出到 MongoDB,但我猜你可以很容易地将它换成 StreamWriter 用于你的 CSV 文件。
private static void ImportValueSetAttributeFile(string filePath)
{
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
// Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
// DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
// Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
var connectionString = ConfigurationManager.ConnectionStrings[0].ConnectionString;
var database = ConfigurationManager.AppSettings["database"];
var mongoAccess = new MongoDataAccess(connectionString, database);
var cdm = new BaseDataManager();
int ind = 0;
for (int i = 0; i < result.Tables.Count; i++)
{
int row_no = 1;
while (row_no < result.Tables[ind].Rows.Count) // ind is the index of table
// (sheet name) which you want to convert to csv
{
var currRow = result.Tables[ind].Rows[row_no];
var valueSetAttribute = new ValueSetAttribute()
{
CmsId = currRow[0].ToString(),
NqfNumber = currRow[1].ToString(),
ValueSetName = currRow[2].ToString(),
ValueSetOid = currRow[3].ToString(),
Definition = currRow[4].ToString(),
QdmCategory = currRow[5].ToString(),
Expansion = currRow[6].ToString(),
Code = currRow[7].ToString(),
Description = currRow[8].ToString(),
CodeSystem = currRow[9].ToString(),
CodeSystemOid = currRow[10].ToString(),
CodeSystemVersion = currRow[11].ToString()
};
cdm.AddRecords<ValueSetAttribute>(valueSetAttribute, "ValueSetAttributes");
row_no++;
}
ind++;
}
}