Code to upload an excel file and read it in a dataset - asp.net

I have a requirement , where in I have to upload an .xlsx file on the server and then read the file into a data table using c# , asp.net.
I need to get your file onto the server.
I need to open the (uploaded?) file.
I need to read the contents of the file, applying any nessesary data parsing.
I need to display the results in a gridview.
Help would be appreciated.

Related

When i'm trying to create microsoft text driver(.txt , .csv) pointing to some specified .csv file,it don't show the file in Select directory?

Actually my intension is talking with non-conventional database like ms=excel,txt files. When i'm trying to create microsoft text driver in data source pointing to some .csv file and also trying to some file in my drives.. in that select Directory file is not visible to pointing it is in hidden mode What is the problem.
You must point to the directory in ODBC first, then set the options for the defined column header format. The ODBC connection will recognize all text files in the directory, and follow the header formats for the columns you must specify

Access Macro Enabled Excel on Webpage using asp.net

I need to access macro enable and external sourced excel 2003 calculator from asp.net web page. Excel accepts few values and returns some numbers post calculations.
Users are not allowed to amend/access the excel from office hence need web access to use the excel calculator features where user enter the values on webpage and pass to Excel and display results on webpage.
I tried to use sharepoint excel services due to macros's and external source on the excel file it fails excel services capability.
I would appreciate if anyone can point me in right direction
You can try EasyXLS Excel library. It can be integrated in ASP.NET web pages. It reads xls file macro enabled files and includes a calculator similar with the one from MS Excel.
// Create an instance of the class that read XLS files
ExcelDocument workbook = new ExcelDocument();
// Read XLS file
workbook.easy_LoadXLSFile("Excel macro enabled.xls");
// Get a sheet and compute the formulas
xlsWorksheet = (ExcelWorksheet) workbook .easy_getSheetAt(0);
String sError = xlsWorksheet.easy_computeFormulas(workbook, true);
// Check the result of the formula
String result = xlsWorksheet.easy_getExcelTable().easy_getCell(3,0).getFormulaResultValue();
To display the existing values and enter later other values you can use this code:
DataSet ds = workbook.easy_ReadExcelWorksheet_AsDataSet(xlsWorksheet);
and set the DataTable as data source for a GridView for example:
http://www.easyxls.com/manual/FAQ/import-excel-to-gridview.html
EasyXLS has also some open source code (more complex) for displaying an Excel file in ASP.NET pages that can be adjusted to your needs:
http://www.easyxls.com/excel-to-gridview

How to read invariant csv files using c#

I am working on Windows Application development using c#. I want to read a csv file from a directory and imported into sql server database table. I am successfully read and import the csv file data into database table if the file content is uniform. But I am unable to insert the file data with invariant form ex.Actually my csv file delimiter is tab('\t') and after getting individual fields I have a field that contains data like dcc
Name
----
xxx
xxx yyy
xx yy zz
and i rerieved data like xxx,yyy and xx,yy,zz so the insertion becomes problem.
How could i insert the data uniformly into a database table.
It's pretty easy.
Just read file line-by-line. Example on MSDN here:
How to: Read Text from a File
For each line use String.Split Method with your tab as delimiter. Method documentation and sample are here:
String.Split Method (Char[], StringSplitOptions)
Then working insert your data.
If a CSV (or TSV) value contains a delimiter inside of it, then it should be surrounded by quotes. See the spec for more details: https://www.rfc-editor.org/rfc/rfc4180#page-3
So your input file is incorrectly formatted. If you can convince the input provider to fix this issue, that will be the best way to fix the problem. If not, other solutions may include:
visually inspecting and editing the file to fix errors, or
writing your parser program to have enough knowledge of your data expectations that it can correctly "guess" where the real delimiters are.
If I'm understanding you correctly, the problem is that your code is splitting on spaces instead of on tabs. Given you have read in the lines from the file, all you need to do is:
string[] fileLines;//from the file
foreach(string line in fileLines)
{
string[] lineParts=line.Split(new char[]{'\t'});
}
and then do whatever you want with each lineParts. The \t is the tab character.
If you're also asking about writing the lines to a database file...you can just read in tab-delimited files with the Import Export Wizard (assuming you're using Sql Server Mgmt Studio, but I'm sure there are comparable ways to import using other db management software).

xml file back up?

I have a asp.net application which reads and updates and xml file in my file system.I do have the options edit and save.As of now it serves my purpose but what if the user edits the values and saves but for some reason he wants to restore the old values to the xml file.I want to
have an option called reset which will get the old values of the xml file incase the user wants to get back to the default values.How do i create a back up to the original file and how do i call that xml file ??? Can any one suggest whether i am in right path or not?
I am using Linq-to xml here .
Keep a copy of the original XML file. Call it xml_file.bk and when the user clicks "reset" delete the current XML file and make a copy of XML_File.bk to the real name with the .xml extension.
You can use File.Move(sourcefile, destinationFile) to make a copy of the original.
File.Move documentation.

Get file upload data from post data in ASP.NET

I am looping through the posted values on a form with a view to doing something with them (so don't have access to the controls themselves). This is the process I have to take on this project so that is why I'm doing it this way.
On the form I will have a file upload box but I am not sure how I would upload the file that has been selected from it as I can't just do Control.SaveAs(). When I return the posted value using Request.Form.Item[i] I get the file name I chose but not the full path like I would expect.
Can someone point me in the right direction please?
Thanks.
If you want to manipulate the uploaded files directly, and not through a FileUploader control, you should use the Request.Files collection and not the Request.Form
File Upload controls only pass the file name and the contents. I'm not sure why you would need a folder name, especially since the folder name would be for the client - I can't expect that this would have any value to you since you want to save the file on the server.
As I am unsure of your goals, I would recommend using Server.MapPath("~/Folder") to find a suitable folder to save your uploaded files to

Resources