从aspx页面在导入到excel中。 一个c#.net 的aspx页面,其中有一个表格控件,动态加载内容,表格控件有隐藏的列,执行代码导出后隐藏列也会导出到EXCEL上而且表格的样式也会丢失,有什么好办法可以解决导出到EXCEL的这些问题呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只做过Excel导入数据库.没做过导出Eexcel的. /// <summary> /// 导出Excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { Export("application/ms-excel", "Employee information.xls"); } /// <summary> /// 定义导出Excel的函数 /// </summary> /// <param name="FileType"></param> /// <param name="FileName"></param> private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } 程序是你写的吧,需要导出哪些内容,导出内容的格式是什么样子,你完全可以自己控制的,用代码都可以实现的,现在导出为Excel有两种方法(仅仅是我看到的),一种导出的其实不是真正的Excel文件,其实还是一个表格,这个简单,可以理解为一个表格,保存为Excel文件后用Excel打开,还有一种就是调用Office库,导出为真正的Excel文件,这样导出的文件才是真正的Excel文件在任何环境下使用都不会有问题,但是编码比较复杂。你自己斟酌考虑如何导出 public string ExportToExcel(string path, DataTable table) { if (table != null &&table.Rows.Count != 0) { //生成.xls文件完整路径名 object filename = path; object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); myExcel.Application.Workbooks.Add(Nothing); try { //把table中的数据插入excel文件中 int totalCount = 0; int row = table.Rows.Count; int column = table.Columns.Count; for (int i = 0; i < column; i++) { myExcel.Cells[totalCount + 1, 1 + i] = table.Columns[i].ColumnName; } for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { myExcel.Cells[totalCount + 2 + i, 1 + j] = table.Rows[i].ItemArray[j].ToString(); ; } } totalCount = totalCount + row + 4; try { myExcel.ActiveWorkbook._SaveAs(filename, Nothing, Nothing, Nothing, Nothing, Nothing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing); } catch (Exception ex) { //return "Error_Save:Excel文件保存失败!"; return ex.ToString(); } } catch (Exception e) { return "Error_Insert:数据导入Excel失败!"; } foreach (Microsoft.Office.Interop.Excel.WorkbookClass w in myExcel.Workbooks) { w.Close(false, Nothing, Nothing); System.Runtime.InteropServices.Marshal.ReleaseComObject(w); } myExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel); myExcel = null; System.GC.Collect(); return "Sucess"; } else { return "Warning:没有可供导入到Excel的数据!"; } } 隐藏第一列,再导出,导出后设置隐藏列可见 或打开模板,遍历数据集,赋值给单元格 e.Row.Cells[0].Visible = false; this.GridView1.Columns[0].Visible = false; http://topic.csdn.net/u/20110117/16/30b7c131-1755-4497-8a0f-21e58cba8c96.html c#.net中的子函数怎样返回多个整型数值? 理解c#中的readonly,const与enum枚举 C# datagridview 问题 不能改源代码 ListBox 的事件问题,请教 一个C#2005连接oracel10g的问题-急 怎么比较二个都带有GUID的字符串是否相同? 100分,求VSS傻瓜教程,(主要求与.net2003配合使用的部分),越详细越好! 我将一个表邦定在dataGrid控件上,然后对表进行修改,如何将修改过的记录做出标记(在线等) 请问一个C#表格框的简单问题 执行1200条插入语句6秒钟,能加快速度吗? winform中怎么设置默认焦点在一个textbox上
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "Employee information.xls");
}
/// <summary>
/// 定义导出Excel的函数
/// </summary>
/// <param name="FileType"></param>
/// <param name="FileName"></param>
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
{
if (table != null &&table.Rows.Count != 0)
{ //生成.xls文件完整路径名
object filename = path;
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
myExcel.Application.Workbooks.Add(Nothing);
try
{
//把table中的数据插入excel文件中
int totalCount = 0; int row = table.Rows.Count;
int column = table.Columns.Count; for (int i = 0; i < column; i++)
{
myExcel.Cells[totalCount + 1, 1 + i] = table.Columns[i].ColumnName;
} for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
myExcel.Cells[totalCount + 2 + i, 1 + j] = table.Rows[i].ItemArray[j].ToString(); ;
}
}
totalCount = totalCount + row + 4; try
{
myExcel.ActiveWorkbook._SaveAs(filename, Nothing, Nothing, Nothing, Nothing, Nothing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing);
}
catch (Exception ex)
{
//return "Error_Save:Excel文件保存失败!";
return ex.ToString();
}
}
catch (Exception e)
{
return "Error_Insert:数据导入Excel失败!";
}
foreach (Microsoft.Office.Interop.Excel.WorkbookClass w in myExcel.Workbooks)
{
w.Close(false, Nothing, Nothing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(w);
}
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myExcel = null;
System.GC.Collect();
return "Sucess";
}
else
{
return "Warning:没有可供导入到Excel的数据!"; }
}
或打开模板,遍历数据集,赋值给单元格
e.Row.Cells[0].Visible = false;
this.GridView1.Columns[0].Visible = false;
http://topic.csdn.net/u/20110117/16/30b7c131-1755-4497-8a0f-21e58cba8c96.html