excel

C# code
            
public void Export(GridView gv) { string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; string filePath = Server.MapPath("..") + fileName; System.Web.HttpResponse httpResponse = Page.Response; httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); httpResponse.ContentType = "application/excel"; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); gv.RenderControl(hw); System.IO.StreamWriter sw = System.IO.File.CreateText(filePath); sw.Write(tw.ToString()); sw.Close(); DownFile(httpResponse, fileName, filePath); httpResponse.End(); } private bool DownFile(System.Web.HttpResponse Response, string fileName, string fullPath) { try { Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312"); System.IO.FileStream fs = System.IO.File.OpenRead(fullPath); long fLen = fs.Length; int size = 102400;//每100K同时下载数据 byte[] readData = new byte[size];//指定缓冲区的大小 if (size > fLen) size = Convert.ToInt32(fLen); long fPos = 0; bool isEnd = false; while (!isEnd) { if ((fPos + size) > fLen) { size = Convert.ToInt32(fLen - fPos); readData = new byte[size]; isEnd = true; } fs.Read(readData, 0, size);//读入一个压缩块 Response.BinaryWrite(readData); fPos += size; } fs.Close(); System.IO.File.Delete(fullPath); return true; } catch { return false; } }
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
发表于:2008-01-14 17:58:263楼 得分:0
http://dotnet.aspx.cc/ShowDetail.aspx?id=EC5E84EC-68F9-4CD7-9E11-6F5C92027F0B
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
  • wangdetian168
  • 等级:
发表于:2008-01-14 20:02:354楼 得分:0
mark
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
发表于:2008-01-14 20:38:015楼 得分:0
如果不需要“导出Excel"当作真正的Excel 来用的随便用html就可以了。

否则可以使用sql命令来生成,跟sql生成sqlserver 数据库一样的。
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
发表于:2008-01-15 09:49:556楼 得分:0
protected void saveExcel(object sender, EventArgs e)
    {
        
        string fileName=ASPnetMenu2.SelectedItem.Label.ToString().Trim()+"排名";
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312 ";
        Response.ContentType = "application/ms-excel ";//vnd.xls";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlPathEncode(fileName+".xls"));
        // If you want the option to open the Excel file without saving than comment out the line below
        //  Response.Cache.SetCacheability(HttpCacheability.NoCache);
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        // turn off paging 
        //bindGridView();
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.Flush();
        Response.End();
    }
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
发表于:2008-01-15 12:00:287楼 得分:0
3种方法 
1:
C# code
                        
Feli.Data.Stock.CheckStockSubDAO objCheckStockSubDAO = new Feli.Data.Stock.CheckStockSubDAO(); Feli.Data.Stock.CheckStockSubCollection objCheckStockSubCol = objCheckStockSubDAO.GetList(); string strPath = Server.MapPath("..\\Excel\\CheckStock"); string strName = strPath + "\\" + "CheckStock" + "_" + strChkNo + ".xls"; System.IO.FileStream fs = new System.IO.FileStream( strName, System.IO.FileMode.Create,System.IO.FileAccess.Write ); System.IO.StreamWriter sw = new System.IO.StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312")); sw.WriteLine(@"商品编号" + "\t" + "商品名称" + "\t" + "可出库数" + "\t" + "单价"); foreach(Feli.Data.Stock.CheckStockSub objCheckStockSub in objCheckStockSubCol) { sw.WriteLine( objCheckStockSub.GdCd + "\t" + objCheckStockSub.GdName + "\t" + objCheckStockSub.StkQty.ToString() + objCheckStockSub.SalePrc.ToString()); } sw.Close(); Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(strName)); Response.ContentType = "application/msexcel";//指定返回的是一个不能被客户端读取的流,必须被下载 Response.WriteFile(strName); //把文件流发送到客户端 Response.End();
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
发表于:2008-01-15 12:00:568楼 得分:0
2:使用MS的组件和服务 
C# code
                        
System.Data.DataTable dt = CreateExcelTable(); if (dt.Rows.Count <= 0) { Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script language=javascript>alert('没有任何可导出的内容')</script>"); return; } Application Excel; int rowIndex = 2; int colIndex = 0; _Workbook xBk; _Worksheet xSt; Excel = new ApplicationClass(); xBk = Excel.Workbooks.Add(true); xSt = (_Worksheet)xBk.ActiveSheet; //打印横向 xSt.PageSetup.Orientation = XlPageOrientation.xlLandscape; //第一行特殊样式 xSt.get_Range(Excel.Cells[1, 1], Excel.Cells[1, 26]).MergeCells = true;//合并单元格 xSt.get_Range(Excel.Cells[1, 1], Excel.Cells[3, 26]).Borders.LineStyle = 1;//加边框 xSt.get_Range(Excel.Cells[1, 1], Excel.Cells[1, 26]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置居中对齐 xSt.get_Range(Excel.Cells[1, 1], Excel.Cells[1, 26]).Font.Size = 18;//设置字体 Excel.Cells[1, 1] = "人才夹:" + ddlHeroFolder.SelectedItem.Text; //取得标题 foreach (DataColumn col in dt.Columns) { colIndex++; Excel.Cells[2, colIndex] = col.ColumnName; xSt.get_Range(Excel.Cells[2, colIndex], Excel.Cells[2, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignLeft;//设置标题格式为居中对齐 xSt.get_Range(Excel.Cells[2, colIndex], Excel.Cells[2, colIndex]).Font.Bold = true; //xSt.get_Range(Excel.Cells[2, colIndex], Excel.Cells[2, colIndex]).Font.Italic = Convert.ToBoolean(this.chkIta.Checked); xSt.get_Range(Excel.Cells[2, colIndex], Excel.Cells[2, colIndex]).Font.Size = 12; //xSt.get_Range(Excel.Cells[2, colIndex], Excel.Cells[2, colIndex]).Font.Name = this.ddlFont.SelectedValue; //xSt.get_Range(Excel.Cells[2, colIndex], Excel.Cells[2, colIndex]).Width = 150; } //取得表格中的数据 // foreach (DataRow row in dt.Rows) { rowIndex++; colIndex = 0; foreach (DataColumn col in dt.Columns) { colIndex++; Excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString(); xSt.get_Range(Excel.Cells[rowIndex, colIndex], Excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐 Range tempRange = xSt.get_Range(xSt.Cells[rowIndex, 26], xSt.Cells[rowIndex, 27]); string strHyperlinks = row["查看简历"].ToString(); xSt.Hyperlinks.Add(tempRange, strHyperlinks, "", "", "查看简历"); } } xSt.Columns.AutoFit(); Excel.Visible = true; //IO xBk.SaveCopyAs(Server.MapPath(".") + "\\" + "FileName" + ".xls"); /////回收 //ds = null; xBk.Close(false, null, null); Excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel); xBk = null; xSt = null; Excel = null; GC.Collect(); //Dispose(); ////// string path = Server.MapPath("FileName.xls"); System.IO.FileInfo file = new System.IO.FileInfo(path); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", file.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-Excel"; // 把文件流发送到客户端 Response.WriteFile(file.FullName); // 停止页面的执行 Response.End();
修改 删除 举报 引用 回复
进入用户个人空间
加为好友
发送私信
在线聊天
发表于:2008-01-15 12:01:369楼 得分:0
3:Excel的XML格式 

C# code
                        
using System; using System.Text; using System.IO; namespace Test { public partial class XmlExcel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //存放模板文件内容 string fileContent = string.Empty; //模板文件位置 string modleFileName = Server.MapPath(".") + "\\ExcelModleFile.xls"; //生成文件位置 string renderFileName = Server.MapPath(".") + "\\ExcelFile.xls"; //读出并保存模板文件内容 StreamReader sr = new StreamReader(modleFileName, System.Text.Encoding.GetEncoding("gb2312")); fileContent = sr.ReadToEnd(); sr.Close(); //循环生成数据行 StringBuilder sbRowsText = new StringBuilder(1024); sbRowsText.Append("<Row ss:AutoFitHeight=\"0\">"); sbRowsText.Append("<Cell ss:StyleID=\"s24\" ss:HRef=\""); //设置超链接地址 sbRowsText.Append("http://www.126.com/"); sbRowsText.Append("\"><Data ss:Type=\"String\">View</Data></Cell>"); sbRowsText.Append("<Cell ss:StyleID=\"s22\"><Data ss:Type=\"String\">"); //设置内容 sbRowsText.Append("Content"); sbRowsText.Append("</Data></Cell>"); sbRowsText.Append("</Row>"); //保存完整Excel内容的字符串 StringBuilder sbRender = new StringBuilder(); //获得模板内容 sbRender.Append(fileContent); //设置Excel数据行 sbRender.Replace(@"[RowCount]", "3"); //设置Excel标题 sbRender.Replace(@"[Header]", "Title"); //添加数据行 sbRender.Replace(@"[DataRows]", sbRowsText.ToString()); lblXml.Text = sbRender.ToString(); //将内容写入文件 StreamWriter sw = new StreamWriter(renderFileName); sw.Write(sbRender.ToString()); sw.Close(); //将文件输出到客户端 Response.Charset = "GB2312"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(renderFileName)); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(renderFileName); Response.End(); } } }
posted @ 2008-08-13 12:32  ike_li  阅读(1090)  评论(0)    收藏  举报