0
我正在上傳excel文件。我將它轉換爲datatable
,如下所示。上傳Excel表並將其映射到不同的模型類
public static DataTable ConvertExcelFileToDataTable(HttpPostedFileBase upload)
{
// ExcelDataReader works with the binary Excel file, so it needs a FileStream
// to get started. This is how we avoid dependencies on ACE or Interop:
Stream stream = upload.InputStream;
IExcelDataReader reader = null;
if (upload.FileName.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (upload.FileName.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
reader.IsFirstRowAsColumnNames = true;
DataTable dtProductCatalog = reader.AsDataSet().Tables[0];
reader.Close();
return dtProductCatalog;
}
現在我想建立一個映射器類型的事情將包含哪些datatable column
將其轉換到模型。然後將datatable
轉換爲相應的模型列表。我不知道如何開始。
我們如何在一個模型中保持3列,其他4個使用這種方法? – Sana
我已將設計更改爲您所需的。該模型現在是「Model1」。工廠現在採用您想要填充的型號名稱。每個模型都會有自己的可覆蓋的填充方法,該方法使用數據表,以便可以根據需要採用任何列值來填充字段。 – Wheels73
好,是否有任何方式之間的Excel列映射到不同模型的列表直接沒有數據表步 – Sana