CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    16

    Copying Excel sheets

    Hi,

    I need to write a simple program to do the following in a very large and complex Excel worksheet:

    1. Create a new worksheet (tab)

    2. Copy the data from the original worksheet to the new worksheet. When pasting the data, it needs to be as "values only" i.e the formulas and references do not need to be copied across.

    3. Cut this new worksheet containing a copy of the data into a new Excel workbook.


    I am using Excel 2010 and .NET 3.5.

    Thanks

  2. #2
    Join Date
    Jun 2011
    Posts
    16

    Re: Copying Excel sheets

    {
    public class ExcelTest
    {
    static void Main(string[] args)
    {
    Excel.ApplicationClass excelApplicationClass = new Excel.ApplicationClass();
    Excel.Workbook destWorkbook = null;
    Excel.Workbook curWorkBook = null;
    Excel.Worksheet workSheet = null;
    Excel.Worksheet newWorksheet = null;
    //Open the destination WorkBook
    destWorkbook = excelApplicationClass.Workbooks.Open("c:/Receipt Excel.xlsx", false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    //Open the source WorkBook
    curWorkBook = excelApplicationClass.Workbooks.Open("c:/Test Excel.xlsx", false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    //Open the WorkSheet
    workSheet = (Excel.Worksheet)curWorkBook.Sheets[1];
    newWorksheet = (Excel.Worksheet)destWorkbook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);

    //Copy from source to destination
    workSheet.Copy(Missing.Value, newWorksheet);
    // Save both workboooks
    destWorkbook.Save();
    curWorkBook.Save();
    }
    }
    }



    I have managed to work out how to do this, the only thing I need help with is how to paste the data as "Values" only. I do not want to copy the formulas and refernces to the new workbook.

    Any hints?

  3. #3
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: Copying Excel sheets

    Sample code I did the other day

    Code:
    Excel.Worksheet ws = ex.ws;
    Excel.Range rng = (Excel.Range)ws.get_Range("1:1");
    rng.Font.Bold = true;
    
    ws.Cells[1, 1] = "Poum poum";
    ws.Cells[1, 2] = "Type";
    
    rng = (Excel.Range)ws.get_Range("G:H");
    rng.Cells.NumberFormat = @"dddd d MMMM yyyy, HH:mm:ss";
    
    rng = (Excel.Range)ws.get_Range("A:D");
    rng.Cells.NumberFormat = @"@";
    
    rng = (Excel.Range)ws.get_Range("A:M");
    rng.Columns.AutoFit();
    ex.SaveExcelFile();
    ex = null;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured