导出.打开文件excel ,pdf

function toExcel(tablename) //导出到excel 
		{
			var mysheet=new ActiveXObject("OWC.Spreadsheet"); 
			
			with(mysheet) 
			{ 
				DataType = "HTMLData"; 
				HTMLData =tablename.outerHTML; 
				try
				{ 
				//fileDialog.CancelError=true; 
				// ActiveSheet.Cells(1,1).value=""; 
				// ActiveSheet.Cells(2,1).value=""; 
					// ActiveSheet.Cells(34,1).value="导出完毕"; 
					ActiveSheet.Export("导出.xls", 0); 
				
					alert('导出完毕'); 
				}; 
				catch (e)
				{ 
					alert('导出Excel表失败,请确定已安装Excel2000(或更高版本),并且没打开同名xls文件'); 
				}; 
			} 
		}

 

将DataSet生成EXCEL的代码如下:

using system.IO;

        public void ExportResult(DataSet ds)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            DataGrid dg = new DataGrid();
            dg.DataSource = ds.Tables[0];
            dg.DataBind();
            dg.RenderControl(htmlWrite);
            HttpContext.Current.Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.End();
        }

在本机上测试一切OK,能够将生成的excel表格下载。到其他机器上试,竟然没有一台机器可以下载,总是转到另一页面直接打开,有时还会出现错误提示。
一开始以为是IE的设置问题,后来就怀疑是OFFICE的版本问题,我的是2003,而另外的都是2000的。
具体原因,尚不得知,但终于解决了这个问题。就是添上一句:
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=result.xls");

 

string   fileName   =   "..\\..\\Images\\"+currentUser.UserAccount+"场地周报表"   +".xls";  
  if   (File.Exists(Server.MapPath(fileName)))  
  File.Delete(Server.MapPath(fileName))   ;  
   
  StreamWriter   w   =   new   StreamWriter(Server.MapPath(fileName),   false,   System.Text.Encoding.Default);  
  for   (int   i   =   0   ;i<=   grid.Rows.Count   -   1   ;   i++)  
  {  
  for   (   int   j   =   0   ;   j<=   grid.Rows[i].Cells.Count   -1   ;j++   )  
  {  
  w.Write(grid.Rows[i].Cells[j].Text   );  
  w.Write('\t');  
  if((grid.Rows[i].Cells[j].Text   ==   "累计金额")||(grid.Rows[i].Cells[j].Text.EndsWith("小计")))  
  {  
  w.Write("-"   );  
  w.Write('\t');  
  }  
   
  }  
  w.Write("\r\n");  
  }  
   
  w.Flush();    
  w.Close();  
  Response.Redirect(fileName);
//

//
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","attachment; filename=欠品明細リスト印刷.PDF");
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
rpt1.DataSource = dt ;
rpt1.Run();
pdf.Export(rpt1.Document, memStream);
Response.BinaryWrite(memStream.ToArray());
Response.End();


Response.Clear();
Response.Buffer= true;
Response.Charset="Shift-JIS";   
Response.AppendHeader("Content-Disposition","attachment;filename=欠品明細リスト.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("Shift-JIS");//set the output flow as Janpanese
Response.ContentType = "application/ms-excel";//set the output fileflow as excel file
this.EnableViewState = false;   
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ja-JP",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
excel.DataSource = detail.DefaultView;  
excel.DataBind();
//

//

//   
  
excel.RenderControl(oHtmlTextWriter);
string str="<table border=0 cellSpacing=0 cellPadding=1 width=350 align=left ><tr><td  colspan=2 height=30 style=font-size:20px>■■欠品明細リスト■■</td></tr><tr><td  colspan=2 align=left style=font-size:15px>欠品日付:"+nowtime+"</td><td></td><td align=left style=font-size:15px>"+excelBranch+"</td ><td align=left style=font-size:15px>"+excelArea+"</td><td align=left style=font-size:15px>"+excelStore+"</td><td align=left style=font-size:15px>"+excelDiv+"</td><td align=left style=font-size:15px>"+excelLine+"</td></tr></table>";

str += oStringWriter.ToString();
Response.Write(str);
Response.End();
Response.End();

 

你可能感兴趣的:(UI,IE,Excel,J#,Office)