源代码网整理以下public void CreateExcel(DataSet ds,string typeid,string FileName)
源代码网整理以下 {
源代码网整理以下 HttpResponse resp;
源代码网整理以下 resp = Page.Response;
源代码网整理以下 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
源代码网整理以下 resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
源代码网整理以下 string colHeaders= "", ls_item="";
源代码网整理以下 int i=0;
源代码网整理以下 //定义表对象与行对像,同时用DataSet对其值进行初始化
源代码网整理以下 DataTable dt=ds.Tables[0];
源代码网整理以下 DataRow[] myRow=dt.Select("");
源代码网整理以下 // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
源代码网整理以下 if(typeid=="1")
源代码网整理以下 {
源代码网整理以下 //取得数据表各列标题,各标题之间以"t分割,最后一个列标题后加回车符
源代码网整理以下 for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+""t";
源代码网整理以下 colHeaders +=dt.Columns[i].Caption.ToString() +""n";
源代码网整理以下 //向HTTP输出流中写入取得的数据信息
源代码网整理以下 resp.Write(colHeaders);
源代码网整理以下 //逐行处理数据
源代码网整理以下 foreach(DataRow row in myRow)
源代码网整理以下 {
源代码网整理以下 //在当前行中,逐列获得数据,数据之间以"t分割,结束时加回车符"n
源代码网整理以下 for(i=0;i ls_item +=row[i].ToString() + ""t";
源代码网整理以下 ls_item += row[i].ToString() +""n";
源代码网整理以下 //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
源代码网整理以下 resp.Write(ls_item);
源代码网整理以下 ls_item="";
源代码网整理以下 }
源代码网整理以下 }
源代码网整理以下 else
源代码网整理以下 {
源代码网整理以下 if(typeid=="2")
源代码网整理以下 {
源代码网整理以下 //从DataSet中直接导出XML数据并且写到HTTP输出流中
源代码网整理以下 resp.Write(ds.GetXml());
源代码网整理以下 }
源代码网整理以下 }
源代码网整理以下 //写缓冲区中的数据到HTTP头文件中
源代码网整理以下 resp.End();
源代码网整理以下 }
源代码网整理以下2、由datagrid生成
源代码网整理以下public void ToExcel(System.Web.UI.Control ctl)
源代码网整理以下 {
源代码网整理以下 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
源代码网整理以下 HttpContext.Current.Response.Charset ="UTF-8";
源代码网整理以下 HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
源代码网整理以下 HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
源代码网整理以下 ctl.Page.EnableViewState =false;
源代码网整理以下 System.IO.StringWriter tw = new System.IO.StringWriter() ;
源代码网整理以下 System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
源代码网整理以下 ctl.RenderControl(hw);
源代码网整理以下 HttpContext.Current.Response.Write(tw.ToString());
源代码网整理以下 HttpContext.Current.Response.End();
源代码网整理以下 }
源代码网整理以下用法:ToExcel(datagrid1);
源代码网整理以下
3、这个用dataview
源代码网整理以下public void OutputExcel(DataView dv,string str)
源代码网整理以下{
源代码网整理以下 //
源代码网整理以下 // TODO: 在此处添加构造函数逻辑
源代码网整理以下 //
源代码网整理以下 //dv为要输出到Excel的数据,str为标题名称
源代码网整理以下 GC.Collect();
源代码网整理以下 Application excel;// = new Application();
源代码网整理以下 int rowIndex=4;
源代码网整理以下 int colIndex=1;
源代码网整理以下 _Workbook xBk;
源代码网整理以下 _Worksheet xSt;
源代码网整理以下 excel= new ApplicationClass();
源代码网整理以下
源代码网整理以下 xBk = excel.Workbooks.Add(true);
源代码网整理以下
源代码网整理以下 xSt = (_Worksheet)xBk.ActiveSheet;
源代码网整理以下 //
源代码网整理以下 //取得标题
源代码网整理以下 //
源代码网整理以下 foreach(DataColumn col in dv.Table.Columns)
源代码网整理以下 {
源代码网整理以下 colIndex++;
源代码网整理以下 excel.Cells[4,colIndex] = col.ColumnName;
源代码网整理以下 xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
源代码网整理以下 }
源代码网整理以下 //
源代码网整理以下 //取得表格中的数据
源代码网整理以下 //
源代码网整理以下 foreach(DataRowView row in dv)
源代码网整理以下 {
源代码网整理以下 rowIndex ++;
源代码网整理以下 colIndex = 1;
源代码网整理以下 foreach(DataColumn col in dv.Table.Columns)
源代码网整理以下 {
源代码网整理以下 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 = 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 = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
源代码网整理以下 }
源代码网整理以下 else
源代码网整理以下 {
源代码网整理以下 excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
源代码网整理以下 }
源代码网整理以下 }
源代码网整理以下 }
源代码网整理以下 //
源代码网整理以下 //加载一个合计行
源代码网整理以下 //
源代码网整理以下 int rowSum = rowIndex + 1;
源代码网整理以下 int colSum = 2;
源代码网整理以下 excel.Cells[rowSum,2] = "合计";
源代码网整理以下 xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
源代码网整理以下 //
源代码网整理以下 //设置选中的部分的颜色
源代码网整理以下 //
源代码网整理以下 xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
源代码网整理以下 xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
源代码网整理以下 //
源代码网整理以下 //取得整个报表的标题
源代码网整理以下 //
源代码网整理以下 excel.Cells[2,2] = str;
源代码网整理以下 //
源代码网整理以下 //设置整个报表的标题格式
源代码网整理以下 //
源代码网整理以下 xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
源代码网整理以下 xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
源代码网整理以下 //
源代码网整理以下 //设置报表表格为最适应宽度
源代码网整理以下 //
源代码网整理以下 xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
源代码网整理以下 xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
源代码网整理以下 //
源代码网整理以下 //设置整个报表的标题为跨列居中
源代码网整理以下 //
源代码网整理以下 xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
源代码网整理以下 xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
源代码网整理以下 //
源代码网整理以下 //绘制边框
源代码网整理以下 //
源代码网整理以下 xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
源代码网整理以下 xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
源代码网整理以下 xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
源代码网整理以下 xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
源代码网整理以下 xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
源代码网整理以下 //
源代码网整理以下 //显示效果
源代码网整理以下 //
源代码网整理以下 excel.Visible=true;
源代码网整理以下 //xSt.Export(Server.MapPath(".")+""""+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
源代码网整理以下 xBk.SaveCopyAs(Server.MapPath(".")+""""+this.xlfile.Text+".xls");
源代码网整理以下 ds = null;
源代码网整理以下 xBk.Close(false, null,null);
源代码网整理以下
源代码网整理以下 excel.Quit();
源代码网整理以下 System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
源代码网整理以下 System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
源代码网整理以下 System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
源代码网整理以下 xBk = null;
源代码网整理以下 excel = null;
源代码网整理以下 xSt = null;
源代码网整理以下 GC.Collect();
源代码网整理以下 string path = Server.MapPath(this.xlfile.Text+".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();
源代码网整理以下}