DataTable 生成 Word excel文件 同上 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/u/20091127/13/4c7091b8-31d4-44bf-b21c-476cffde7873.html http://www.cnblogs.com/ericfine/archive/2008/10/24/1318923.html 你问了也不结贴,谁还愿意回答你啊http://topic.csdn.net/u/20100925/14/d04a798e-edbb-493d-9ded-ead30e3473bb.html 本帖最后由 net_lover 于 2010-09-26 15:56:19 编辑 private void Export() { string strTempFileName = DateTime.Now.ToString("yyyyMMddHHmmss")+".xls"; string strModName = Server.MapPath(".") + @"\Template.xls"; string strDownFileName = @"./" + strTempFileName; string strFileName = Server.MapPath(".") + @"\" + strTempFileName; Excel.Application m_objExcel = new Excel.Application(); m_objExcel.Workbooks.Open(strModName, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss); Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks; Excel.Workbook m_objBook = m_objExcel.ActiveWorkbook; Excel.Worksheet sheet = (Excel.Worksheet)m_objBook.ActiveSheet; //for (int i = 0; i < total.Rows.Count; i++) //{ // for (int j = 0; j < total.Columns.Count; j++) // { // Excel.Range range = sheet.get_Range(GetAix(j + 1, i + 1), miss); // range.set_Value(miss, total.Rows[i][j].ToString()); // //string aix = y + x.ToString(); // //Excel.Range range = sheet.get_Range(aix, miss); // //range.set_Value(miss, text); // } //} //Excel.ApplicationClass excel; //Excel._Workbook xBk; //Excel._Worksheet xSt; //Excel.Range r = xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]); for (int i = 0; i < total.Rows.Count; i++) { for (int j = 0; j < total.Columns.Count; j++) { Excel.Range range = sheet.get_Range(m_objExcel.Cells[i + 1, j + 1], m_objExcel.Cells[i + 1, j + 1]); if (total.Columns[j].ColumnName == "成绩") { range.set_Value(miss, total.Rows[i][j].ToString()); } else { range.set_Value(miss, total.Rows[i][j].ToString()); } } } m_objBook.SaveAs(@strFileName, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges, miss, miss, miss, miss); m_objExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objExcel); System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBooks); System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBook); System.Runtime.InteropServices.Marshal.ReleaseComObject((object)sheet); m_objExcel = null; m_objBooks = null; m_objBook = null; sheet = null; GC.Collect(); Response.Redirect(strDownFileName, false); } protected static System.Data.DataTable total; private object miss = Missing.Value; private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; protected void Page_Load(object sender, EventArgs e) { #region 初始化绑定 if (!IsPostBack) { //创建和声明DataTable对象 total = new DataTable(); //给DataTable对象添加DataColumn对象 total.Columns.Add(new DataColumn("班组", typeof(string))); total.Columns.Add(new DataColumn("姓名", typeof(string))); total.Columns.Add(new DataColumn("学科", typeof(string))); total.Columns.Add(new DataColumn("成绩", typeof(string))); for (int i = 0; i < 16; i++) { //声明DataRow对象 DataRow dr = total.NewRow(); //给DataRow对象中的各列赋值 dr["班组"] = "一班" + i + ":" + 1; dr["姓名"] = " 李宏" + i + ":" + 1; dr["学科"] = "语文 " + i + ":" + 1; dr["成绩"] = "98000001222222222222222222"; //将DataRow对象添加到DataTable对象中 total.Rows.Add(dr); dr = total.NewRow(); dr["班组"] = "一班" + i + ":" + 2; dr["姓名"] = " 刘京生" + i + ":" + 2; dr["学科"] = "语文 " + i + ":" + 2; dr["成绩"] = "00000000000000000000000096"; total.Rows.Add(dr); } } #endregion }using System.Reflection; 再次给你说明一下,Excel.Application这种方法不适合在虚拟主机上使用,如果没有自己的服务器,你直接生成html的兼容格式或者xml格式答案已经在上一个帖子了 public static void TableToExcel(System.Data.DataTable tb) { string Filename = "CardExcel"; System.Web.HttpContext context = System.Web.HttpContext.Current; if ((tb != null)) { context.Response.Clear(); context.Response.Charset = "GB2312"; context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); context.Response.ContentType = "application/ms-excel"; context.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename, System.Text.Encoding.GetEncoding("GB2312")) + ".xls\""); CultureInfo cult = new CultureInfo("zh-CN", true); StringWriter sw = new StringWriter(cult); HtmlTextWriter htw = new HtmlTextWriter(sw); DataGrid dgrid = new DataGrid(); dgrid.DataSource = tb.DefaultView; dgrid.AllowPaging = false; dgrid.DataBind(); htw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">"); dgrid.RenderControl(htw); context.Response.Write(sw.ToString()); context.Response.End(); } } 关于工厂模式模式编程的问题! 我在网上下载的对联广告 代码,在IE其他版本测试都正常,但是在IE9里面无法显示,哪位大神帮忙改一下,万分感激! |M| 如何删除DataTable中的相同行 谢谢 odbc连接池如果控制连接数的问题? 请教:如何用Columns["columnsname"].Expression筛选数据? gridview 的删除提示怎么实现? 如何在DATAGRID里显示DATALIST .net多语言网站访问各自数据库问题 VS.net 写的ASP.net程序出问题。 简单问题,怎么判断一个字符串string 类型,是否是数值或者可以转换为数值? 酒店管理后台用户身份识别的问题 gridview 中的textBox的问题
private void Export()
{
string strTempFileName = DateTime.Now.ToString("yyyyMMddHHmmss")+".xls";
string strModName = Server.MapPath(".") + @"\Template.xls";
string strDownFileName = @"./" + strTempFileName;
string strFileName = Server.MapPath(".") + @"\" + strTempFileName; Excel.Application m_objExcel = new Excel.Application();
m_objExcel.Workbooks.Open(strModName, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss); Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
Excel.Workbook m_objBook = m_objExcel.ActiveWorkbook;
Excel.Worksheet sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
//for (int i = 0; i < total.Rows.Count; i++)
//{
// for (int j = 0; j < total.Columns.Count; j++)
// {
// Excel.Range range = sheet.get_Range(GetAix(j + 1, i + 1), miss);
// range.set_Value(miss, total.Rows[i][j].ToString()); // //string aix = y + x.ToString();
// //Excel.Range range = sheet.get_Range(aix, miss);
// //range.set_Value(miss, text);
// }
//} //Excel.ApplicationClass excel;
//Excel._Workbook xBk;
//Excel._Worksheet xSt; //Excel.Range r = xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]);
for (int i = 0; i < total.Rows.Count; i++)
{
for (int j = 0; j < total.Columns.Count; j++)
{
Excel.Range range = sheet.get_Range(m_objExcel.Cells[i + 1, j + 1], m_objExcel.Cells[i + 1, j + 1]);
if (total.Columns[j].ColumnName == "成绩")
{
range.set_Value(miss, total.Rows[i][j].ToString());
}
else
{
range.set_Value(miss, total.Rows[i][j].ToString());
}
}
}
m_objBook.SaveAs(@strFileName, miss, miss, miss, miss,
miss, Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, miss, miss, miss, miss);
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)sheet);
m_objExcel = null;
m_objBooks = null;
m_objBook = null;
sheet = null;
GC.Collect(); Response.Redirect(strDownFileName, false);
}
protected static System.Data.DataTable total; private object miss = Missing.Value;
private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
protected void Page_Load(object sender, EventArgs e)
{
#region 初始化绑定
if (!IsPostBack)
{
//创建和声明DataTable对象
total = new DataTable();
//给DataTable对象添加DataColumn对象
total.Columns.Add(new DataColumn("班组", typeof(string)));
total.Columns.Add(new DataColumn("姓名", typeof(string)));
total.Columns.Add(new DataColumn("学科", typeof(string)));
total.Columns.Add(new DataColumn("成绩", typeof(string)));
for (int i = 0; i < 16; i++)
{
//声明DataRow对象
DataRow dr = total.NewRow();
//给DataRow对象中的各列赋值
dr["班组"] = "一班" + i + ":" + 1;
dr["姓名"] = " 李宏" + i + ":" + 1;
dr["学科"] = "语文 " + i + ":" + 1;
dr["成绩"] = "98000001222222222222222222";
//将DataRow对象添加到DataTable对象中
total.Rows.Add(dr);
dr = total.NewRow();
dr["班组"] = "一班" + i + ":" + 2;
dr["姓名"] = " 刘京生" + i + ":" + 2;
dr["学科"] = "语文 " + i + ":" + 2;
dr["成绩"] = "00000000000000000000000096";
total.Rows.Add(dr);
} }
#endregion
}using System.Reflection;
Excel.Application
这种方法不适合在虚拟主机上使用,如果没有自己的服务器,你直接生成html的兼容格式或者xml格式
答案已经在上一个帖子了
{
string Filename = "CardExcel";
System.Web.HttpContext context = System.Web.HttpContext.Current;
if ((tb != null))
{
context.Response.Clear();
context.Response.Charset = "GB2312";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
context.Response.ContentType = "application/ms-excel";
context.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename, System.Text.Encoding.GetEncoding("GB2312")) + ".xls\""); CultureInfo cult = new CultureInfo("zh-CN", true);
StringWriter sw = new StringWriter(cult);
HtmlTextWriter htw = new HtmlTextWriter(sw);
DataGrid dgrid = new DataGrid();
dgrid.DataSource = tb.DefaultView;
dgrid.AllowPaging = false;
dgrid.DataBind();
htw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">");
dgrid.RenderControl(htw);
context.Response.Write(sw.ToString());
context.Response.End();
}
}