We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Excel Upload

Is it possible to trigger an upload dialog box from Aspose.Excel and directly load the selected file into Aspose.Excel object?

If so, could you post a code sample?

Thanks for all of your help.

So far, my demo of Aspose.Excel for management is going very well. With a few modifications to the code from the demo, I was able to populate a dataset from a SQL Server, and then output an XL sheet using a designer sheet.

The most impressive features noted were the speed of the spreadsheet creation, the simplicity of the object model, and the multiple export options (browser/excel/file).

The next step is to demo the upload. We need to be sure that we can upload an XL file, validate the data in the cells, and then transform it into an array.

Best Regards,
Michael Minadeo
IBM Global Services
Nissan Account

Dear Michael,

Aspose.Excel is a non-UI componet, so you have to write your own code to upload your file to server’s directory or stream. Then Aspose.Excel can load it.

In asp.net, you can use HtmlInputFile component to upload excel file.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;


private void Button1_Click(object sender, System.EventArgs e)
{
System.IO.Stream ioStream = File1.PostedFile.InputStream;
int fileLength = File1.PostedFile.ContentLength;
byte[] buffer = new byte[fileLength];
ioStream.Read(buffer, 0, fileLength);
MemoryStream stream = new MemoryStream(buffer);

Excel excel = new Excel();
excel.Open(stream);
excel.Save(“abc.xls”, SaveType.OpenInExcel, FileFormatType.Default, this.Response);
}
}

Laurence,

Thank you!

Aspose’s level of support is great!

We appreciate all of your help.

Regards,
Michael Minadeo
IBM Global Services
Nissan Account

Laurence,

I tried the code you suggested and got the following error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 108: //Loading the spreadsheet
Line 109:
Line 110: System.IO.Stream ioStream = InputPartFile.PostedFile.InputStream;
Line 111: int fileLength = InputPartFile.PostedFile.ContentLength;
Line 112: byte[] buffer = new byte[fileLength];

How does the HtmlInputFile get instanciated, if it does not occur globally?

Regards,
Michael Minadeo
IBM Global Services
Nissan Account

Dear Michael,

Sorry, I forgot to remind you. I have also meet this problem at the first time to use HtmlInputFile.

Note that the Enctype attribute on the tag must be set to “multipart/form-data”.

And please instanciate the HtmlInputFile as other components, such as Button.

If problem still exists, please refer to VS.net quickstart. In “How do I…” tab, find the “ASP.NET” section, click the link of “Upload a File to an ASP.NET application?”.
It provides a complete sample of file upload.

Laurence,

Thank you for all of your help. Your suggestion works very well.

Once the file is uploaded, does Aspose.Excel provide a method to output the Excel file data to a dataset, as it does when loading the spreadsheet object?

Best Regards,
Michael Minadeo
IBM Global Services
Nissan Account

Dear Michael,

You can try the following method:

When the file is uploaded, use Exce.Open to load it, then use the Cells.ExportDataTable to export data in the worksheet.

@mike,
Aspose.Excel is deprecated now and is replaced with Aspose.Cells which is much more ahead in performance and features when compared to older versions. Aspose.Cells provides rich features to import and export data to/from MS Excel file. Have a look at the following sample code and relevant articles for more details.

string filePath = dataDir + "Book1.xlsx";

// Instantiating a Workbook object
Workbook workbook = new Workbook(filePath);

// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];

// Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable
DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0, 11, 2, true);

foreach (DataRow r in dataTable.Rows)
{
    foreach (DataColumn c in dataTable.Columns)
    {
        Double value = r.Field<Double>(c);
        Console.Write(value + "    ");
    }
    Console.WriteLine();
}

For more details go through the following links:
Export Data from Worksheet
Import Data into Worksheet

Download the latest version of Aspose.Cells for .NET from the following link:
Aspose.Cells for .NET (Latest Version)

You can download the latest demos here.