|
|
|
start date: Thu, 23 Aug 2007 06:40:02 -0700,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
Lamis
|
|
2
Patrice http://www.chez.com/scribe/
|
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
Date:Thu, 23 Aug 2007 06:40:02 -0700
Author:
|
Re: Upload/DownLoad from Excel
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" a crit dans le message de news:
09375668-3C58-4EE8-9020-C7AC5A20AEC5@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
Date:Thu, 23 Aug 2007 15:53:10 +0200
Author:
|
|
|