473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Upload/DownLoad from Excel

HI,

I am doing an aspx application and need to let my users be able to edit the
information in my database by the page. My first Idea was to let them export
the information to an excel file at local machine where they can edit it och
then upload the file by the page again to save down their changes down to the
database.
Now, this is the code I am using to Upload from sql to excel :
HttpResponse response = HttpContext.Current.Response;

// first let's clean up the response.object
response.Clear();
response.Charset = "";
string filename = "myExcel.xls";
// set the response mime type for excel
DataSet ds = RulesFacade.GetRuleList();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
filename + "\"");

// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
response.Write(sw.ToString());
response.End();
}
}
this works perfectly and I have an excel file on my desktop. The things is
that the files looks a little bit strange, with letters like #¤%& etc, not
everywhere but in many places.

And when I try to upload the file (the file I just uploaded to my local
machine) whith this code
public void UploadFromExcell(string filePath,strFileName)
{
try
{
/*strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source="+Server.MapPath(".\\" + StrFileName)+";" +
"Extended Properties=Excel 8.0;";*/
File1.PostedFile.SaveAs(Server.MapPath(".\\" + StrFileName));
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source="+filePath+";" +
"Extended Properties=Excel 8.0;";

//System.Diagnostics.Debug.WriteLine(Server.MapPath( ".\\" + StrFileName));

//You must use the $ after the object you reference in the spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM
[TrandaxRegler$]",strConn);

DataSet myDataSet = new DataSet();

myCommand.Fill(myDataSet, "ExcelInfo");
foreach(DataRow r in myDataSet.Tables[0].Rows)
{
System.Diagnostics.Debug.WriteLine(r[0]);
System.Diagnostics.Debug.WriteLine(r[1]);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}

}
I get an exception that the file is not correctly configured. is it cause it
only has one sheet?? this code works perfectly with another excel file I have
on my machine, but the one that works has more than one sheet...
Any idea how to solve my problem?? download to excel from sql, do changes in
excel file and upload the save file and save it to database.
--
LZ
Aug 23 '07 #1
1 2839
1) use BinaryWrite

2) Always mention the exact message you have
Don't catch exception if you don't do anything else than ASP.NET do.
Before just dump the connection string to see what is its final value and if
it looks correct

I'm doing thse kind of things for "mass updates" by the application
administrators. If this is more directed to end useers you could of course
crzeate a web form including posisbkly all needed checks etc...

--
Patrice
"Lamis" <La***@discussions.microsoft.coma écrit dans le message de news:
09**********************************@microsoft.com...
HI,

I am doing an aspx application and need to let my users be able to edit
the
information in my database by the page. My first Idea was to let them
export
the information to an excel file at local machine where they can edit it
och
then upload the file by the page again to save down their changes down to
the
database.
Now, this is the code I am using to Upload from sql to excel :
HttpResponse response = HttpContext.Current.Response;

// first let's clean up the response.object
response.Clear();
response.Charset = "";
string filename = "myExcel.xls";
// set the response mime type for excel
DataSet ds = RulesFacade.GetRuleList();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
filename + "\"");

// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
response.Write(sw.ToString());
response.End();
}
}
this works perfectly and I have an excel file on my desktop. The things is
that the files looks a little bit strange, with letters like #¤%& etc, not
everywhere but in many places.

And when I try to upload the file (the file I just uploaded to my local
machine) whith this code
public void UploadFromExcell(string filePath,strFileName)
{
try
{
/*strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source="+Server.MapPath(".\\" + StrFileName)+";" +
"Extended Properties=Excel 8.0;";*/
File1.PostedFile.SaveAs(Server.MapPath(".\\" + StrFileName));
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source="+filePath+";" +
"Extended Properties=Excel 8.0;";

//System.Diagnostics.Debug.WriteLine(Server.MapPath( ".\\" + StrFileName));

//You must use the $ after the object you reference in the spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM
[TrandaxRegler$]",strConn);

DataSet myDataSet = new DataSet();

myCommand.Fill(myDataSet, "ExcelInfo");
foreach(DataRow r in myDataSet.Tables[0].Rows)
{
System.Diagnostics.Debug.WriteLine(r[0]);
System.Diagnostics.Debug.WriteLine(r[1]);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}

}
I get an exception that the file is not correctly configured. is it cause
it
only has one sheet?? this code works perfectly with another excel file I
have
on my machine, but the one that works has more than one sheet...
Any idea how to solve my problem?? download to excel from sql, do changes
in
excel file and upload the save file and save it to database.
--
LZ

Aug 23 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5814
by: Prakash | last post by:
Hi, We face problems uploading excel (with macros) documents using HTML File Upload. The file contents are corrupted while viewing the same. However, we are able to upload excel (w/o. macros)...
1
2213
by: Daniel | last post by:
Hi, can u explain to me what is the path need to use in order to upload and download the file based on below solution: 1.Set up a folder that both the web app and SQL server can get to. 2.Do a...
5
6559
by: Seok Bee | last post by:
Dear Experts, I currently trying to use the FileUpload control from asp.net 2.0 to upload files. The uploading of the file I would like to store it in the Access Database. Unfortunately, I've no...
0
1506
by: Pk | last post by:
Hi, I want to download and upload the data from acess database into excel file or from excel to database. So how can i use "Excel.Application" to do this type of job. So please help me soon. ...
3
7985
by: c676228 | last post by:
Hi everyone, I will develop a program to enroll a group of people on-line. Since we don't have number limitation for the people in the group. I am wondering if there is any company allow people...
0
9766
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
1
1443
by: Tlholo | last post by:
Here are the requirements for the project. I have also included the other project that I am working on and is also giving me some problems. i need code examples. Project 1 This program actually...
0
1012
by: jayce | last post by:
Hi I am using an ADODB connection to link up an Access database to my core spreadsheet tool. I have created a loop to repeatedly: - download data from Access - analyse it in Excel, with data...
7
3719
by: dragiton | last post by:
SA Upload SQL Database variable types (image upload and storage) I am having trouble with the SA Upload utility. The following code used to work correctly. However, I lost my database and had to...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.