RT

解决方案 »

  1.   

    数据我已经准备好了,谢谢各位
    public DataView dv;
            public DataTable dt=new DataTable ();
    private void Form1_Load(object sender, System.EventArgs e)
    {
    //创建数据源

    dt.Columns .Add ("ID",Type.GetType ("System.Int32"));
    dt.Columns .Add ("Color",Type.GetType ("System.String"));
    DataRow dr=dt.NewRow ();
    dr[0]=10;
    dr[1]="Red";
    dt.Rows .Add (dr);
    dr=dt.NewRow ();
    dr[0]=10;
    dr[1]="Green";
    dt.Rows .Add (dr); dr=dt.NewRow ();
    dr[0]=10;
    dr[1]="Green";
    dt.Rows .Add (dr); //创建DataGridTableStyle
    System.Windows .Forms .DataGridTableStyle tbstyle=new DataGridTableStyle ();
     DataGridTextBoxColumn mc; for(int i=0;i<dt.Columns .Count ;i++)
    {
    mc=new DataGridTextBoxColumn ();
    if (i==dt.Columns .Count-1)
    { }
    mc.MappingName =dt.Columns [i].ColumnName ;
    mc.HeaderText =dt.Columns [i].ColumnName ;
    tbstyle.GridColumnStyles .Add (mc);
    } //绑定数据
    this.dataGrid1 .TableStyles .Add (tbstyle);
    this.dataGrid1 .DataSource =dt;
                dv=dt.DefaultView; }
      

  2.   

    public void ToExcel(System.Web.UI.Control ctl,string FileName)
    {
                HttpContext.Current.Response.Charset ="UTF-8";
                HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
                HttpContext.Current.Response.ContentType ="application/ms-excel";
                HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+FileName+".xls");
                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,@"c\meng.xls");
      

  3.   

    http://www.cnblogs.com/lingyun_k/archive/2005/11/07/271068.html
      

  4.   

    谢谢楼上两位兄弟,我要的是winform的呀
      

  5.   

    我做了一个,供参考:
        Public Sub DataViewtToExcel(ByVal dv As DataView, ByVal fileName As String, ByVal getEmail As String)
            Dim xlApp As Excel.Application
            Dim xlBook As Excel.Workbook
            Dim xlSheet As Excel.Worksheet
            Dim xbookname As String = ""
            Dim rowIndex, colIndex As Integer
            Dim myTable As System.Data.DataTable
            Dim Row As DataRow
            Dim Col As DataColumn        xlApp = New Excel.Application
            xbookname = fileName
            If File.Exists(xbookname) Then
                xlBook = xlApp.Workbooks.Add(xbookname)
            Else
                xlBook = xlApp.Workbooks.Add()
                'xlBook.SaveAs(xBookname)
            End If        xlSheet = xlBook.Worksheets("sheet1")
            '获取筛选后的结果,以作为输出源。        myTable = dv.Table            '确认列标题
            Dim title(17) As String
            Dim t As Integer
            title(0) = "ID"
            title(1) = "RequestID"
            title(2) = "Vendor Name"
            title(3) = "SO No"
            title(4) = "Line No"
            title(5) = "Watch Model"
            title(6) = "Movement Item"
            title(7) = "Request Qty"
            title(8) = "Total Request Qty"
            title(9) = "Total JDE Order Qty"
            title(10) = "Balance Qty"
            title(11) = "PO No"
            title(12) = "Customer No"
            title(13) = "Request Date"
            title(14) = "Move Del.Dt"
            title(15) = "Attn"
            title(16) = "Ref No"        t = 1 '隐藏第一列ID
            colIndex = 0
            For Each Col In myTable.Columns
                colIndex = colIndex + 1
                xlApp.Cells(1, colIndex) = title(t)
                t += 1
            Next        Dim intcol As Integer
            rowIndex = 1
            For Each Row In myTable.Rows
                rowIndex = rowIndex + 1
                colIndex = 0
                '隐藏第一列ID列.所以intcol从1开始.
                For intcol = 1 To myTable.Columns.Count - 1
                    colIndex = colIndex + 1
                    xlApp.Cells(rowIndex, colIndex) = Row(myTable.Columns(intcol).ColumnName)
                Next
            Next        With xlSheet
                '设标题为宋体字
                .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "宋体"
                '标题字体加粗
                .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True
                '设表格边框样式
                .Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1
                '设表格字体
                .Range(.Cells(2, 1), .Cells(rowIndex, colIndex)).Font.Name = "宋体"
                .Range(.Cells(2, 1), .Cells(rowIndex, colIndex)).Font.Size = 12
                .Range(.Cells(2, 1), .Cells(rowIndex, colIndex)).Font.Bold = False
            End With         xlBook.Saved = True
            xlApp.UserControl = False
            xlBook.SaveAs(xbookname)
             xlApp.Quit()
          End Sub
      

  6.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=4EB79F05-B9A4-4E8A-836F-864393F40405
      

  7.   

    给你一些参考,这个是listview的,datatable和这一样的.自己作一些个修改就是了
    用C#快速往Excel写数据
      本示例是用于将ListView中的内容倒入到Excel,与常用的逐单元格写不同的是,本例子采用数据写入到range的方法。该方法效率明显较高。   Excel.Application app = new Excel.ApplicationClass();
      if( app == null)
      {
      MessageBox.Show("Excel无法启动");
      return;
      }
      app.Visible = true;
      Excel.Workbooks wbs = app.Workbooks;
      Excel.Workbook wb = wbs.Add(Missing.Value);
      Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
      Excel.Range r = ws.get_Range("A1","H1");
      object [] objHeader = {"标题1","标题2","标题3",
      "标题4","标题5","标题6",
      "标题7","标题8"};
      r.Value = objHeader;
      if (lv.Items.Count >0)
      {
      r = ws.get_Range("A2",Missing.Value);  
      object [,] objData = new Object[this.lv.Items.Count,8];
      foreach(ListViewItem lvi in lv.Items)
      {
      objData[lvi.Index,0] = lvi.Text;
      objData[lvi.Index,1] = lvi.SubItems[1].Text;
      objData[lvi.Index,2] = lvi.SubItems[2].Text;
      objData[lvi.Index,3] = lvi.SubItems[3].Text;
      objData[lvi.Index,4] = lvi.SubItems[4].Text;
      objData[lvi.Index,5] = lvi.SubItems[5].Text;
      objData[lvi.Index,6] = lvi.SubItems[6].Text;
      objData[lvi.Index,7] = lvi.SubItems[7].Text;
      }
      r = r.get_Resize(lv.Items.Count,8);
      r.Value = objData;
      r.EntireColumn.AutoFit(); 
      }
      app = null; 
      

  8.   

    直接把这个贴子的代码拷过来就可以了
    http://community.csdn.net/Expert/topic/4379/4379324.xml?temp=.7426264http://blog.csdn.net/flygoldfish