试试这个,不过是web的,不知window行不行:
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = ""; //Remove the charset from the Content-Type header.
Page.EnableViewState = false; //Turn off the view state.
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid2.RenderControl(hw); 
DataGrid1.RenderControl(hw);     //Get the HTML for the control.
Response.Write(tw.ToString()); //Write the HTML back to the browser.
Response.End();         //End the response.

解决方案 »

  1.   

    see
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05152003.asp?frame=true
      

  2.   

    // <summary>
    /// 输出到Excel
    /// </summary>
    /// <param name="dv">DataView </param>
    /// <param name="path">文件名</param>
    /// <param name="title1">标题</param>
    /// <param name="heji"></param>
    public void CreateExcel(DataView dv,string path,string title1,string heji)
    {
    try
    {
    FileInfo fi =new FileInfo(path);
    fi.Delete();
    }
    catch
    {
    } Excel.Application excel =new Excel.ApplicationClass();
    object oMissing =System.Reflection.Missing.Value;
    Excel.Workbook book =excel.Workbooks.Add(oMissing);

    int rowIndex =3;
    int colIndex =0;

    excel.Cells[1,1] =title1;
    foreach(DataColumn dc in dv.Table.Columns)
    {
    colIndex++;
    excel.Cells[rowIndex,colIndex] =dc.ColumnName;
    ((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =18.8;
    } foreach(DataRow row in dv.Table.Rows)
    {
    rowIndex++;
    colIndex =0;
    foreach(DataColumn dc in dv.Table.Columns)
    {
    colIndex ++;
    excel.Cells[rowIndex,colIndex] = row[dc.ColumnName].ToString();
    ((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =18.8;
    }
    }
    excel.Cells[rowIndex+1,colIndex] =heji; Excel.Sheets sheets =book.Worksheets;
    Excel._Worksheet worksheet =(Excel._Worksheet)sheets.get_Item(1);
                
    Excel.Range range;
    range =worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,colIndex]);
    range.MergeCells =true;//合并
    range.HorizontalAlignment =Excel.XlHAlign.xlHAlignCenter;//居中
    range.Font.Bold =true;
    range.Font.Size =14;
    book.Saved =true;
    excel.UserControl =false; worksheet.SaveAs(path,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);
    excel.Quit();
    oMissing =null;
    sheets =null;
    worksheet =null;
    book =null;
    excel =null;
    GC.Collect();
    }
      

  3.   


            Dim xlsapp As Application = New Application()
            Dim xlsbook As Workbook
            Dim xlssheet As Worksheet
            Dim odertype() As String
            Dim oder As String
            Dim i As Int32
            Dim j As Int32 = 1
            Dim Mint_i As Int32, Mint_j As Int32 = 4, t As Int32 = 1
            Dim Mobj_drow As DataRow, Mstr_msgarray() As String, Mstr() As String, Mstr_data() As String, Mstr_msg1 As String
            xlsapp.AlertBeforeOverwriting = False
            xlsapp.DisplayAlerts = False
            xlsapp.Application.Visible = False
            xlsbook = xlsapp.Workbooks.Add()
            xlssheet = xlsbook.ActiveSheet
            'pageheader
            xlssheet.Select()
            xlssheet.Cells.Font.Size = 8
            xlssheet.Cells.Font.Name = "Times New Roman"        xlssheet.Cells(1, 1).Value = "Dealers Mailing List"
            xlssheet.Range("A1:I1").Merge()
            xlssheet.Range("A1:I1").Font.Size = 14
            xlssheet.Range("A1:I1").Font.Name = "Times New Roman"
            xlssheet.Range("A1:I1").Font.Bold = True
            With xlsapp.Selection()
                .HorizontalAlignment = 3
            End With
            With xlssheet.PageSetup
                .Zoom = 80
                .Orientation = 2
                .PaperSize = 1
            End With        'cloumn width
            xlssheet.Columns("A:I").EntireColumn.AutoFit()
            xlssheet.Columns("A:A").columnwidth = 34
            xlssheet.Columns("B:B").columnwidth = 11
            xlssheet.Columns("C:C").columnwidth = 10.25
            xlssheet.Columns("D:D").columnwidth = 34
            xlssheet.Columns("E:E").columnwidth = 16.38
            xlssheet.Columns("F:F").columnwidth = 2.63
            xlssheet.Columns("G:G").columnwidth = 6.88
            xlssheet.Columns("H:H").columnwidth = 9.0
            xlssheet.Columns("I:I").columnwidth = 17.25
            xlsapp.Cells.RowHeight = 15.75        t += 1
            xlssheet.Cells(t, 1) = "Product Line:   " + getprod()
            'xlssheet.Cells(t, 2) = Session("prod")
            t += 1
            xlssheet.Cells(t, 1) = "Has not placed stock in (months):   " + Session("month")
            ' xlssheet.Cells(t, 2) = Session("month")
            t += 1
            xlssheet.Cells(t, 1) = "Territory:   " + Session("terriname")
            xlssheet.Cells(t, 9).HorizontalAlignment = -4152
            xlssheet.Cells(t, 9) = "Page No:   " & j        xlssheet.Range("A" & t, "I" & t).Borders(9).LineStyle = 1
            t += 1
            Mstr_msg = "Dealers" & Chr(9) & "Tel" & Chr(9) & "Fax" & Chr(9) & "Address" & Chr(9) & "City" & Chr(9) & "State" & Chr(9) & "Zip" & Chr(9) & "Territory" & Chr(9) & "Contact Person"
            Mstr_msgarray = Split(Mstr_msg, Chr(9))
            For Mint_i = 1 To Mstr_msgarray.Length            xlssheet.Cells(t, Mint_i) = Mstr_msgarray(Mint_i - 1)
            Next
            'xlssheet.Range("A" & t - 1, "I" & t - 1).Borders(9).LineStyle = 1
            xlssheet.Range("A" & t, "I" & t).Select()
            With xlsapp.Selection
                .Interior.ColorIndex = 15
                '.Interior.Pattern = 9
                '.Interior.PatternColorIndex = xlAutomatic
                .Borders(9).LineStyle = 1
                .HorizontalAlignment = 3
            End With
            t += 1
            Mint_j = t        For Each Mobj_drow In DataSet1.Tables("AAA").Rows            If (Mint_j) Mod 35 = 0 Then
                    j += 1
                    xlssheet.Range("A" & Mint_j, "I" & Mint_i - 1).Borders(9).LineStyle = 1                Mint_j += 1
                    xlssheet.HPageBreaks.Add(xlsapp.Range("A" & Mint_j))
                    xlssheet.Cells(Mint_j, 9).HorizontalAlignment = -4152
                    xlssheet.Cells(Mint_j, 9) = "Page No:   " & j                xlssheet.Range("A" & Mint_j, "I" & Mint_i - 1).Borders(9).LineStyle = 1
                    Mint_j += 1
                    For Mint_i = 1 To Mstr_msgarray.Length
                        xlssheet.Cells(Mint_j, Mint_i) = Mstr_msgarray(Mint_i - 1)
                    Next                xlssheet.Range("A" & Mint_j, "I" & Mint_j).Select()
                    With xlsapp.Selection
                        .borders(9).linestyle = 1
                        .Interior.ColorIndex = 15
                        .HorizontalAlignment = 3
                    End With
                    Mint_j += 1
                End If
                Mstr_msg1 = Mobj_drow.Item("Dealers") & Chr(9) & Mobj_drow.Item("Tel") & Chr(9) & Mobj_drow.Item("Fax") & Chr(9) & Mobj_drow.Item("Address") & Chr(9) & Mobj_drow.Item("City") & Chr(9) & Mobj_drow.Item("State") & Chr(9) & Mobj_drow.Item("custzip") & Chr(9) & Mobj_drow.Item("terrname") & Chr(9) & Mobj_drow.Item("contactperson")
                Mstr_data = Split(Mstr_msg1, Chr(9))
                For Mint_i = 1 To Mstr_msgarray.Length
                    If Mint_i = 1 Then
                        Mstr = Split(Mstr_data(Mint_i - 1), "/")
                        xlssheet.Cells(Mint_j, Mint_i) = Mstr(1)
                    ElseIf Mint_i = 6 Or Mint_i = 7 Or Mint_i = 8 Then
                        xlssheet.Cells(Mint_j, Mint_i).HorizontalAlignment = 3
                        xlssheet.Cells(Mint_j, Mint_i) = Mstr_data(Mint_i - 1)
                    Else
                        xlssheet.Cells(Mint_j, Mint_i) = Mstr_data(Mint_i - 1)
                    End If
                Next
                Mint_j += 1
            Next
            If Mint_j Mod 35 <> 0 Then
                xlssheet.Range("A" & Mint_j + (35 - Mint_j Mod 35), "I" & Mint_i - 1).Borders(9).LineStyle = 1
            End If
            xlsbook.SaveAs(path)
            xlsbook.Close()
            xlsbook = Nothing
            xlsapp.Quit()
            xlsapp = Nothing
             GC.Collect()
      

  4.   

    写成HTML表格, 保存为XLS就行了