在.net中将数据导入到EXCEL中,在客户机上运行程序时,客户机不能打开EXCEL,服务器上却启动了EXCEL。以前测试时,在服务器上装有.net的程序,代码运行正常没有出现问题,但重新装服务器后,只在服务器上安装了从微软网站上下的.net SDK,就出现了问题,代码如下:
//将模板文件复制到一个新文件中
string filename;
filename=Server.MapPath("\\web_file\\xls\\") +System.DateTime.Now.Ticks.ToString() + ".xls";
//将模板文件copy到新位置,建议实际开发时用相对路径,如Application.StartupPath.Trim()+"\\report\\normal.xls"
string filenameold=Server.MapPath("\\web_file\\统计表1模板.xlt");
File.Copy(filenameold,filename);
//打开复制后的文件
Excel.Application myExcel=new Excel.Application ( );
object missing=System.Reflection.Missing.Value;
//Excel.Workbook myBook;
myExcel.Visible=true;
//打开新文件
myExcel.Application.Workbooks.Open(filename,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
//逐行写入数据,数组中前五行列标题,忽略,EXCEl中的行列号是从1开始
int row=6,DGRow;
Excel_num=0;
myExcel.Cells[row++,1]="'一、";
DGRow=DataGrid1.Items.Count;
if (DGRow>0)
{
DGtoExcelSheet(DataGrid1,myExcel,DGRow,row,"Label5");
row=row+DGRow;
}
//myBook.Saved=true;
//myExcel.UserControl = false;
//myBook.Save();
//myExcel.Quit();
//missing=null;
//myBook=null;
//myExcel=null;
GC.Collect(); RemoveFiles(Server.MapPath("\\web_file\\xls\\"));
}各位大虾帮帮忙了
//将模板文件复制到一个新文件中
string filename;
filename=Server.MapPath("\\web_file\\xls\\") +System.DateTime.Now.Ticks.ToString() + ".xls";
//将模板文件copy到新位置,建议实际开发时用相对路径,如Application.StartupPath.Trim()+"\\report\\normal.xls"
string filenameold=Server.MapPath("\\web_file\\统计表1模板.xlt");
File.Copy(filenameold,filename);
//打开复制后的文件
Excel.Application myExcel=new Excel.Application ( );
object missing=System.Reflection.Missing.Value;
//Excel.Workbook myBook;
myExcel.Visible=true;
//打开新文件
myExcel.Application.Workbooks.Open(filename,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
//逐行写入数据,数组中前五行列标题,忽略,EXCEl中的行列号是从1开始
int row=6,DGRow;
Excel_num=0;
myExcel.Cells[row++,1]="'一、";
DGRow=DataGrid1.Items.Count;
if (DGRow>0)
{
DGtoExcelSheet(DataGrid1,myExcel,DGRow,row,"Label5");
row=row+DGRow;
}
//myBook.Saved=true;
//myExcel.UserControl = false;
//myBook.Save();
//myExcel.Quit();
//missing=null;
//myBook=null;
//myExcel=null;
GC.Collect(); RemoveFiles(Server.MapPath("\\web_file\\xls\\"));
}各位大虾帮帮忙了
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
DataGrid1.RenderControl(oHtmlTextWriter);
Response.AppendHeader("Content-Disposition", "attachment;filename= "+ Title + ".xls");
Response.Write(oStringWriter.ToString());
Response.End();
服务器启动excel是正常的。>>以前测试时,在服务器上装有.net的程序,代码运行正常没有出现问题,但重新装服务器后,只在服务器上安装了从微软网站上下的.net SDK,就出现了问题,什么问题?有没有安装office?>>代码如下:
...
GC.Collect();
...excel的gc有bug,完整的写法如下:
GC.Collect();
System.Diagnostics.Process[] myProcesses ;
myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach(System.Diagnostics.Process myProcess in myProcesses)
{
myProcess.Kill();
}
>>...
Response.ContentType = "application/vnd.ms-excel";
...
DataGrid1.RenderControl(oHtmlTextWriter);
...你有没有重载RenderControl这个函数?
//导入excel中
private void excel_btn_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
string str =star_text.Text.Trim()+"--"+end_text.Text.Trim(); //设置文件名
Response.AppendHeader("Content-Disposition","attachment;filename="+str+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
MyDataGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]",conn);
DataSet ds = new DataSet();
adp.Fill(ds,"Book1");
ExlDataGrid.DataSource = ds.Tables["Book1"].DefaultView;
ExlDataGrid.DataBind(); 按照这个做你就明白了
不过你考虑一下用xmlhttp可以否?