आप स्मृति में फ़ाइल को पढ़ने के लिए HttpPostedFile
की InputStream
संपत्ति का उपयोग कर सकते हैं।
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
के लिए:
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 संस्करण है:
यहाँ एक उदाहरण से पता चलता है जो EPPlus
का उपयोग कर एक HttpPostedFile
की IO.Stream
से एक DataTable
बनाने का तरीका बताया पूर्णता के लिए, यहां एएसपीएक्स है:
<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>
स्रोत
2012-09-14 08:05:05
अरे टिम, त्वरित प्रतिक्रिया के लिए धन्यवाद। मेरा मानना है कि यह आवश्यक होगा कि होस्टिंग सर्वर पर माइक्रोसॉफ्ट ऑफिस स्थापित किया जाए। यदि हां, तो क्या आप कृपया मुझे बता सकते हैं कि क्या कोई और काम है? यदि नहीं, तो कोई समस्या नहीं है। मैं इसका परीक्षण करूंगा और आपको बता दूंगा। – Chandra
@ कंधरा: जैसा कि मैंने बताया है कि मैं ['EPPLus'] (http://epplus.codeplex.com/) का उपयोग कर रहा हूं जो [एलजीपीएल लाइसेंस] (http://epplus.codeplex.com/license) के तहत नि: शुल्क है। मैं इसे गर्मजोशी से अनुशंसा कर सकता हूं और मैं अकेला नहीं हूं। http://stackoverflow.com/a/2603625/284240 (स्वीकृत उत्तर जनवरी के बगल में) –
त्वरित प्रतिक्रिया के लिए धन्यवाद !! मैं देखता हूं कि यह ओपन सोर्स कोड है जो इसे हमारे उच्च प्रोफ़ाइल ग्राहक को फिर से धक्का देना मुश्किल होगा। क्या शुद्ध .NET कोड का उपयोग करके इसे कार्यान्वित करना संभव है? – Chandra