数据集输出word或excel文件

    技术2022-05-11  79

    webformResponse.Clear();    Response.Buffer= true;    Response.Charset="GB2312";   Response.AppendHeader("Content-Disposition","attachment;filename="+文件名+".xls",System.Text.Encoding.UTF8));    //attachment --- 作为附件下载   //inline --- 在线打开   //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)   //进行进行编码,以解决文件名乱码的问题   Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文   Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。    //Response.ContentType是输出流的 HTTP MIME 类型   //Response.ContentType = "Response.ContentType";   //Response.ContentType     --- word文件   //application/vnd.ms-excel --- excel文件   this.EnableViewState = false;       System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);   System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);   this.Your_DataGrid.RenderControl(oHtmlTextWriter);    //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以   Response.Write(oStringWriter.ToString());   Response.End();

     winform

    using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.IO;

    namespace CangKuGuanli.BLL{  public class ExportXLS    {      private  string tempStr;      private  string str;       public void ExportDataGridViewToExcel(DataGridView dataGridview1)        {          SaveFileDialog saveFileDialog = new SaveFileDialog();          saveFileDialog.Filter = "Execl files (*.xls)|*.xls";          saveFileDialog.FilterIndex = 0;          saveFileDialog.RestoreDirectory = true;          saveFileDialog.CreatePrompt = false;          saveFileDialog.Title = "导出Excel文件到";                    if (saveFileDialog.ShowDialog() == DialogResult.Cancel)              return;          Stream myStream = saveFileDialog.OpenFile();          StreamWriter sw = new StreamWriter(myStream,System.Text.Encoding.GetEncoding("gb2312")); //gb2312

              try            {                //写标题                 for (int i = 0; i< dataGridview1.ColumnCount; i++)                {                    if (i == 0) dataGridview1.Columns[i].HeaderText = "序号";                    if (i > 0)                    {                        str += "/t";                    }                                       str += dataGridview1.Columns[i].HeaderText;                }

                    sw.WriteLine(str);                //写内容          1               for(int j = 0; j < dataGridview1.Rows.Count-1; j++) 2               { 3                   tempStr = ""; 4                5                   for (int k = 0; k < dataGridview1.Columns.Count; k++) 6                   { 7                       8                       if( k == 0 ) 9                           dataGridview1.Rows[j].Cells[k].Value = j + 1; 10                        11                       if (k > 0) 12                       { 13                           tempStr += "/t"; 14                       } 15                 tempStr += dataGridview1.Rows[j].Cells    [k].Value.ToString(); 17                   } 18                   sw.WriteLine(tempStr); 19              } 20               sw.Close(); 21                myStream.Close();            }          catch (Exception e)           {                MessageBox.Show(e.ToString());           }          finally           {               sw.Close();               myStream.Close();           }        }

         }}


    最新回复(0)