您可以使用HttpPostedFile
的InputStream
屬性文件讀入到內存中。
這裏是它展示瞭如何從一個HttpPostedFile
的IO.Stream
使用EPPlus
創建DataTable
一個例子:
protected void UploadButton_Click(Object sender, EventArgs e)
{
if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".xlsx")
{
using (var excel = new ExcelPackage(FileUpload1.PostedFile.InputStream))
{
var tbl = new DataTable();
var ws = excel.Workbook.Worksheets.First();
var hasHeader = true; // adjust accordingly
// add DataColumns to DataTable
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
tbl.Columns.Add(hasHeader ? firstRowCell.Text
: String.Format("Column {0}", firstRowCell.Start.Column));
// add DataRows to DataTable
int startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row = tbl.NewRow();
foreach (var cell in wsRow)
row[cell.Start.Column - 1] = cell.Text;
tbl.Rows.Add(row);
}
var msg = String.Format("DataTable successfully created from excel-file. Colum-count:{0} Row-count:{1}",
tbl.Columns.Count, tbl.Rows.Count);
UploadStatusLabel.Text = msg;
}
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
這裏的VB.NET版本:
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If (FileUpload1.HasFile AndAlso IO.Path.GetExtension(FileUpload1.FileName) = ".xlsx") Then
Using excel = New ExcelPackage(FileUpload1.PostedFile.InputStream)
Dim tbl = New DataTable()
Dim ws = excel.Workbook.Worksheets.First()
Dim hasHeader = True ' change it if required '
' create DataColumns '
For Each firstRowCell In ws.Cells(1, 1, 1, ws.Dimension.End.Column)
tbl.Columns.Add(If(hasHeader,
firstRowCell.Text,
String.Format("Column {0}", firstRowCell.Start.Column)))
Next
' add rows to DataTable '
Dim startRow = If(hasHeader, 2, 1)
For rowNum = startRow To ws.Dimension.End.Row
Dim wsRow = ws.Cells(rowNum, 1, rowNum, ws.Dimension.End.Column)
Dim row = tbl.NewRow()
For Each cell In wsRow
row(cell.Start.Column - 1) = cell.Text
Next
tbl.Rows.Add(row)
Next
Dim msg = String.Format("DataTable successfully created from excel-file Colum-count:{0} Row-count:{1}",
tbl.Columns.Count, tbl.Rows.Count)
UploadStatusLabel.Text = msg
End Using
Else
UploadStatusLabel.Text = "You did not specify an excel-file to upload."
End If
End Sub
對於完整的緣故,這裏的ASPX:
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
嘿添,感謝您的快速反應。我相信這將需要Microsoft Office安裝在託管服務器上。如果是,請問我是否還有其他解決方法?如果沒有,那麼沒問題。我會測試這個並讓你知道。 – Chandra
@Chandra:正如我在[LGPL許可證](http://epplus.codeplex.com/license)下使用['EPPLus'](http://epplus.codeplex.com/)所述。我可以熱烈地推薦它,我不是唯一的。 http://stackoverflow.com/a/2603625/284240(在接受的答案Jan本身旁邊) –
感謝您的快速響應!我看到這是開源代碼,這又難以將其推向我們的高知名度客戶。是否有可能使用純.NET代碼實現這一點? – Chandra