如何从文件input中读取excel文件而不保存文件到服务器

我正在接收客户端上传到服务器的Excel文件,我正在查看的所有示例都显示了如何保存并读取它。

我需要做的是从网页和/或FileReader中接收excel文件,并将其保存到数据表中,从Excel工作表的第一行保留列名。

下面的代码正是我所需要的,但是它没有显示如何将文件读入数据表而不先将其保存到存储器中。

我需要保存excel文件并对其进行处理,以确保所有列名都是正确的,并且每行中的数据都是正确的。处理完这些信息后,我将开始将其保存到sql数据库的过程。

如何将Excel文件作为stream保存到数据表中,同时保留列名?

**注意表名不重要,我将只处理Excel书的第一张表格,每行中的列名和数据types是最重要的。

代码被引用: http : //www.aspsnippets.com/Articles/Read-and-Import-Excel-File-into-DataSet-or-DataTable-using-C-and-VBNet-in-ASPNet.aspx

using System; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data; using System.Data.OleDb; using System.IO; using System.Configuration; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName); string FolderPath = ConfigurationManager.AppSettings["FolderPath"]; string FilePath = Server.MapPath(FolderPath + FileName); FileUpload1.SaveAs(FilePath); Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text); } } private void Import_To_Grid(string FilePath, string Extension, string isHDR) { string conStr=""; switch (Extension) { case ".xls": //Excel 97-03 conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString; break; case ".xlsx": //Excel 07 conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString; break; } conStr = String.Format(conStr, FilePath, isHDR); OleDbConnection connExcel = new OleDbConnection(conStr); OleDbCommand cmdExcel = new OleDbCommand(); OleDbDataAdapter oda = new OleDbDataAdapter(); DataTable dt = new DataTable(); cmdExcel.Connection = connExcel; //Get the name of First Sheet connExcel.Open(); DataTable dtExcelSchema; dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); connExcel.Close(); //Read Data from First Sheet connExcel.Open(); cmdExcel.CommandText = "SELECT * From [" + SheetName + "]"; oda.SelectCommand = cmdExcel; oda.Fill(dt); connExcel.Close(); //Bind Data to GridView GridView1.Caption = Path.GetFileName(FilePath); GridView1.DataSource = dt; GridView1.DataBind(); } 

您可以使用EPPlus执行以下操作:

 protected void btnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { DataTable dataTable = new DataTable(); using (MemoryStream mStream = new MemoryStream(fileContents)) { using (var excelPackage = new ExcelPackage(mStream)) { ExcelWorksheet firstSheet = excelPackage.Workbook.Worksheets.First(); var endAddress = firstSheet.Dimension.End; dataTable.TableName = firstSheet.Name; ExcelRange headerRange = firstSheet.Cells[1, 1, 1,endAddress.Column ]; //Add columns using headers foreach (var cell in headerRange) { dataTable.Columns.Add(cell.Value.ToString()); //You can hardcode whatever type you need to here } //Add Data: for (int rowIdx = 2; rowIdx <= endAddress.Row; rowIdx++) { DataRow dataRow = dataTable.NewRow(); for (int colIdx = 1; colIdx <= endAddress.Column; colIdx++) { dataRow[colIdx - 1] = firstSheet.Cells[rowIdx, colIdx].Value; } dataTable.Rows.Add(dataRow); } } } //Now Do whatever you want with your DataTable: GridView1.DataSource = dataTable; GridView1.DataBind(); } } 

唯一的问题是你没有从导入的文件中读取任何types的模式,所以你没有得到任何types(上面的例子最后是每一行填充一般的objecttypes)。

如果您需要一个强types的数据表,并且事先知道DataTable需要的列的types,那么您可以切换出创build列的行。 例如:

 dataTable.Columns.Add(cell.Value.ToString(),typeof(int)); 

如果你事先不知道列的types, 仍然需要强types的表,那么我想你可以用单元格的Style.Numberformat属性来Style.Numberformat一些东西来决定将哪个type传递给列构造器。