想调用excel打开一文件,然后另存为其它文件,请问如何做?

解决方案 »

  1.   

    Sub SHtAdd()
    Application.DisplayAlerts = False
        For Each sht In Sheets
            If sht.Name = "hjsong" Then sht.Delete
        Next
    Sheets.Add(after:=Sheets(Sheets.Count)).Name = "hjsong"
    Application.DisplayAlerts = True
    End SubSub GET_hjsong_MX()
    Dim files
    Dim i%
    Dim m&
    Dim a&, b&
    Dim sht As Worksheet
    Dim Chjs As Worksheet
    Dim wb As WorkbookApplication.ScreenUpdating = FalseSHtAddSet Chjs = ThisWorkbook.Sheets("hjsong")
        files = Application.GetOpenFilename("所有文件(*.xls),*.xls", , , , True)
        If Not IsArray(files) Then
        MsgBox "没有选定工作薄!~"
        Application.DisplayAlerts = False
        Chjs.Delete
        Application.DisplayAlerts = True
        Exit Sub
        End If
        
        m = 2
        For i = LBound(files) To UBound(files)
    Set wb = Workbooks.Open(files(i))
    N = Application.Substitute(wb.Name, ".xls", "")
        For Each sht In wb.Sheets
        a = GETrow(sht)
        b = GETcol(sht)
        If a > 1 And b > 1 And sht.Name = "明细" Then
            sht.Cells.AutoFilter
            If i = LBound(files) Then
            sht.Range(sht.Cells(1, 1), sht.Cells(1, b)).Copy Chjs.Cells(1, 2)
            For t = 1 To b
            Chjs.Cells(1, t + 1).ColumnWidth = sht.Cells(1, t).ColumnWidth
            Next t
            End If
            
            sht.Range(sht.Cells(2, 1), sht.Cells(a, b)).Copy Chjs.Cells(m, 2)
            Chjs.Cells(m, 1) = N
            m = GETrow(Chjs) + 1
        End If
        Next sht
        ActiveWorkbook.Close False
        Next i
    Application.ScreenUpdating = True
    End Sub
      

  2.   

    xlApplication.Workbooks("book1.xls").Save
      

  3.   

    添加Microsoft Office 11.0 Object Library引用
    using System;
    using Excel = Microsoft.Office.Interop.Excel;
    namespace csexcel
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // TODO: 在此处添加代码以启动应用程序
    //
    Excel.ApplicationClass app = new Excel.ApplicationClass();
    object opt = System.Reflection.Missing.Value;
    app.Workbooks.Add( "c:\\1.xls" );
    Excel.Worksheet ws =(Excel.Worksheet) app.Workbooks[1].Worksheets[1];
    Excel.Range columns = (Excel.Range)ws.Columns[1,6];
    System.Console.Write( columns.Count.ToString() );
    app.Visible = true;
    Excel.Workbook wb  = app.Workbooks[1];
    wb.SaveAs( "d:\\2.xls" , opt , opt , opt , opt,opt, Excel.XlSaveAsAccessMode.xlNoChange , opt,opt,opt,opt,opt);
    if( app != null )
    {
    app.Quit();
    }
    }
    }
    }