zoukankan      html  css  js  c++  java
  • ASP.NET导入EXCEL方法汇总

    1. 1、由dataset生成  
    2. public void CreateExcel(DataSet ds,string typeid,string FileName)  
    3.    {  
    4.    HttpResponse resp;  
    5.    resp = Page.Response;  
    6.    resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");  
    7.    resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);     
    8.    string colHeaders= "", ls_item="";  
    9.    int i=0;  
    10.    //定义表对象与行对像,同时用DataSet对其值进行初始化  
    11.    DataTable dt=ds.Tables[0];  
    12.    DataRow[] myRow=dt.Select("");  
    13.    // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件  
    14.    if(typeid=="1")  
    15.     {  
    16.     //取得数据表各列标题,各标题之间以 分割,最后一个列标题后加回车符  
    17.     for(i=0;i     colHeaders+=dt.Columns[i].Caption.ToString()+" ";  
    18.     colHeaders +=dt.Columns[i].Caption.ToString() +" ";     
    19.     //向HTTP输出流中写入取得的数据信息  
    20.     resp.Write(colHeaders);  
    21.     //逐行处理数据    
    22.     foreach(DataRow row in myRow)  
    23.      {  
    24.      //在当前行中,逐列获得数据,数据之间以 分割,结束时加回车符   
    25.      for(i=0;i      ls_item +=row[i].ToString() + " ";       
    26.      ls_item += row[i].ToString() +" ";  
    27.      //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据      
    28.      resp.Write(ls_item);  
    29.      ls_item="";  
    30.     }  
    31.    }  
    32.    else  
    33.     {  
    34.     if(typeid=="2")  
    35.      {  
    36.      //从DataSet中直接导出XML数据并且写到HTTP输出流中  
    37.      resp.Write(ds.GetXml());  
    38.     }      
    39.    }  
    40.    //写缓冲区中的数据到HTTP头文件中  
    41.    resp.End();  
    42. }  
    43. 2、由datagrid生成  
    44. public void ToExcel(System.Web.UI.Control ctl)    
    45.    {  
    46.    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");  
    47.    HttpContext.Current.Response.Charset ="UTF-8";      
    48.    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;  
    49.    HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.msexcel/  
    50. msword  
    51.    ctl.Page.EnableViewState =false;     
    52.    System.IO.StringWriter tw = new System.IO.StringWriter() ;  
    53.    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);  
    54.    ctl.RenderControl(hw);  
    55.    HttpContext.Current.Response.Write(tw.ToString());  
    56.    HttpContext.Current.Response.End();  
    57. }  
    58. 用法:ToExcel(datagrid1);  
    59. 3、这个用dataview  
    60. public void OutputExcel(DataView dv,string str)  
    61. {  
    62.    //  
    63.    // TODO: 在此处添加构造函数逻辑  
    64.    //  
    65.                            //dv为要输出到Excel的数据,str为标题名称  
    66.    GC.Collect();  
    67.    Application excel;// = new Application();  
    68.    int rowIndex=4;  
    69.    int colIndex=1;  
    70.    _Workbook xBk;  
    71.    _Worksheet xSt;  
    72.    excel= new ApplicationClass();  
    73.     
    74.    xBk = excel.Workbooks.Add(true);  
    75.      
    76.    xSt = (_Worksheet)xBk.ActiveSheet;  
    77.    //  
    78.    //取得标题  
    79.    //  
    80.    foreach(DataColumn col in dv.Table.Columns)  
    81.     {  
    82.     colIndex++;  
    83.     excel.Cells[4,colIndex] = col.ColumnName;  
    84.     xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格  
    85. 式为居中对齐  
    86.    }  
    87.    //  
    88.    //取得表格中的数据  
    89.    //  
    90.    foreach(DataRowView row in dv)  
    91.     {  
    92.     rowIndex ++;  
    93.     colIndex = 1;  
    94.     foreach(DataColumn col in dv.Table.Columns)  
    95.      {  
    96.      colIndex ++;  
    97.      if(col.DataType == System.Type.GetType("System.DateTime"))  
    98.       {  
    99.       excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");  
    100.       xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment =  
    101. XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐  
    102.      }  
    103.      else  
    104.       if(col.DataType == System.Type.GetType("System.String"))  
    105.       {  
    106.       excel.Cells[rowIndex,colIndex] = "’"+row[col.ColumnName].ToString();  
    107.       xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment =  
    108. XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐  
    109.      }  
    110.      else  
    111.       {  
    112.       excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();  
    113.      }  
    114.     }  
    115.    }  
    116.    //  
    117.    //加载一个合计行  
    118.    //  
    119.    int rowSum = rowIndex + 1;  
    120.    int colSum = 2;  
    121.    excel.Cells[rowSum,2] = "合计";  
    122.    xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;  
    123.    //  
    124.    //设置选中的部分的颜色  
    125.    //  
    126.    xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();  
    127.    xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有  
    128. 56种  
    129.    //  
    130.    //取得整个报表的标题  
    131.    //  
    132.    excel.Cells[2,2] = str;  
    133.    //  
    134.    //设置整个报表的标题格式  
    135.    //  
    136.    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;  
    137.    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;  
    138.    //  
    139.    //设置报表表格为最适应宽度  
    140.    //  
    141.    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();  
    142.    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();  
    143.    //  
    144.    //设置整个报表的标题为跨列居中  
    145.    //  
    146.    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();  
    147.    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;  
    148.    //  
    149.    //绘制边框  
    150.    //  
    151.    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;  
    152.    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight =  
    153. XlBorderWeight.xlThick;//设置左边线加粗  
    154.    xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight =  
    155. XlBorderWeight.xlThick;//设置上边线加粗  
    156.    xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight =  
    157. XlBorderWeight.xlThick;//设置右边线加粗  
    158.    xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight =  
    159. XlBorderWeight.xlThick;//设置下边线加粗  
    160.    //  
    161.    //显示效果  
    162.    //  
    163.    excel.Visible=true;  
    164.    //xSt.Export(Server.MapPath(".")  
    165. +"file://"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportForma  
    166. t.ssExportHTML/);  
    167.    xBk.SaveCopyAs(Server.MapPath(".")+"file://"+this.xlfile.Text+".xls/ ");  
    168.    ds = null;  
    169.             xBk.Close(false, null,null);  
    170.      
    171.             excel.Quit();  
    172.             System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);  
    173.             System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);  
    174.     System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);  
    175.             xBk = null;  
    176.             excel = null;  
    177.    xSt = null;  
    178.             GC.Collect();  
    179.    string path = Server.MapPath(this.xlfile.Text+".xls");  
    180.    System.IO.FileInfo file = new System.IO.FileInfo(path);  
    181.    Response.Clear();  
    182.    Response.Charset="GB2312";  
    183.    Response.ContentEncoding=System.Text.Encoding.UTF8;  
    184.    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名  
    185.    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));  
    186.    // 添加头信息,指定文件大小,让浏览器能够显示下载进度  
    187.    Response.AddHeader("Content-Length", file.Length.ToString());  
    188.      
    189.    // 指定返回的是一个不能被客户端读取的流,必须被下载  
    190.    Response.ContentType = "application/ms-excel";  
    191.      
    192.    // 把文件流发送到客户端  
    193.    Response.WriteFile(file.FullName);  
    194.    // 停止页面的执行  
    195.     
    196.    Response.End();  
    197. }  
  • 相关阅读:
    选择排序——Selection Sort
    Android使用AIDL跨进程通信
    Android Gradle Plugin Version和Gradle Version 对应关系
    error: device unauthorized —— android studio 链接不上虚拟机
    Touch事件传递机制 Android
    Activity生命周期
    Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
    Error:fatal: Not a git repository (or any of the parent directories): .git
    Installation failed with message...It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.
    Ajax 学习总结
  • 原文地址:https://www.cnblogs.com/ranran/p/3897118.html
Copyright © 2011-2022 走看看