多谢指点!
我可能对你的“临时的HtmlForm”理解的不对,我是在工程中添加一个空的web页面充当“临时的HtmlForm”,代码如下:
C1.Web.UI.Controls.C1GridView.C1GridView T1 = (C1.Web.UI.Controls.C1GridView.C1GridView)this.form1.FindControl("T1");
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
WebForm3 tmpForm = new WebForm3();
tmpForm.Controls.Add(T1);
T1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
不知道,我错在哪里啦,好像还是报错:
类型“C1GridView”的控件“T1”必须放在具有 runat=server 的窗体标记内。
不过,没关系,我已经用另外的方法解决了导入EXCEL的功能。
public string OutputExcel(System.Data.DataTable dt_CH,System.Data.DataTable dt, string strTitle, string FilePath)
{
DateTime beforeTime = System.DateTime.Now;
int titleColorindex = 15;
Excel.Application excel;
Excel._Workbook xBk;
Excel._Worksheet xSt;
int rowIndex = 4;
int colIndex = 1;
//excel = new Excel.ApplicationClass();
excel = new Excel.Application();
xBk = excel.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.ActiveSheet;
//取得列标题
foreach (DataRow row in dt_CH.Rows)
{
colIndex++;
excel.Cells[4, colIndex] = row["FIELD_NAME_CH"].ToString();// col.Caption;// col.ColumnName;
}
//取得表格中的数据
foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 1;
foreach (DataRow row2 in dt_CH.Rows)
{
DataColumn col = dt.Columns[row2["ALIAS_NAME"].ToString()];
colIndex++;
if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if (col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
//xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
DateTime afterTime = DateTime.Now;
string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
xBk.Close(null, null, null);
excel.Workbooks.Close();
excel.Quit();
if (xSt != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xSt = null;
}
if (xBk != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
xBk = null;
}
if (excel != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
}
GC.Collect();//垃圾回收
Excel.Application excel2 = new Excel.Application(); //引用Excel对象
Excel.Workbook book = excel2.Application.Workbooks.Add("c:\\" + filename);
excel2.Visible = true; //使Excel可视 } |