3

How can i copy one worksheet values and paste an another worksheet on within a workbook in aspose.cell using asp.net with c#?

Thanks and regard, Parthiban K.

Parthiban
  • 35
  • 2
  • 4

1 Answers1

3

I work as Social Media developer at Aspose. Aspose.Cells offer multiple options to achieve your desired results. You can Export the data from one worksheet to another and then import the data to the destination worksheet using . You can also copy all the data as range to the destination worksheet. Check the following samples:

Copy the Range from First Worksheet to destination worksheet

//Open the workbook
Workbook workbook = new Workbook("book1.xlsx");

//Select source worksheet
Worksheet worksheet = workbook.Worksheets[0];


//Select Destination Worksheet
Worksheet destSheet = workbook.Worksheets[1]; 

//Get the range of cells with all the data from source worksheet
Aspose.Cells.Range sourceRange = worksheet.Cells.MaxDisplayRange;

//Create a range with same row and column count as source worksheet
Aspose.Cells.Range destRange = destSheet.Cells.CreateRange(sourceRange.FirstRow, sourceRange.FirstColumn, sourceRange.RowCount, sourceRange.ColumnCount);

//Select Paste Options
PasteOptions options = new PasteOptions();

options.PasteType = PasteType.All;

//Copy the range from source worksheet to destination.
destRange.Copy(sourceRange, options);

//Save the updated worksheet
workbook.Save("book1.xlsx");

Export data from one worksheet and import to another

//Open the workbook
Workbook workbook = new Workbook("book1.xlsx");

//Select source worksheet
Worksheet worksheet = workbook.Worksheets[0];

//Exporting the of worksheet to DataTable
DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.MaxRow, worksheet.Cells.MaxColumn, true);

//Select Destination Worksheet
Worksheet destSheet = workbook.Worksheets[1];

//Import data to destination worksheet
destSheet.Cells.ImportDataTable(dataTable, true, "A1");

//Save the updated worksheet
workbook.Save("book1.xlsx");
  • :) Thanks for your kind repose Nausherwan Aslam and when i got from the datatable values displayed by following type, The type of cell value is #Value. it's not a original value but i used some formula on that particular cell and here I'm used one column but each cell contains some formulas totally 2369 rows.. How can i read formula based excel worksheet values? – Parthiban May 09 '14 at 08:46
  • I checked the above scenario and it works fine with the latest version (http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/entry540242.aspx). If you are using an older version, download and try the latest version. Also, you can use Workbook.CalculateForumla() method to recalculate the formula values before exporting data from datatable. If you still face any issue, please share your file here (or with our technical support team using Aspose forums) for further testing. – Nausherwan Aslam May 09 '14 at 09:48
  • I'm getting this error Object reference not set to an instance of an object. Error in calculating cell A951 in Worksheet OUTPUT. Here is my code.. Workbook workbook = new Workbook(); workbook.Open(Server.MapPath(".") + @"\Report\Book1.xls");Worksheet worksheet = workbook.Worksheets["OUTPUT"];workbook.CalculateFormula();DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.MaxRow, worksheet.Cells.MaxColumn, true); – Parthiban May 09 '14 at 10:25
  • Dear Nausherwan Aslam, I have one Excel template it contains two worksheets(Input and Output). Output worksheet have some formula, It need to get some specific cells values to another new worksheet. Here I'm going to get some values from the db and it'll going to write two work sheets(Sheet1 and Sheet2) on that Excel template and the Output worksheet will get some input's on that two worksheets finally the Output worksheet successfully generated output values then i have to read the Output worksheet total values while when i read the Output cell values shows the dummy value.. It's my problem. – Parthiban May 09 '14 at 10:45
  • Well, could you prepare a sample sheet with the formulas for which you getting the issue and share with me. We need to test the issue as I am unable to reproduce the issue you mentioned – Nausherwan Aslam May 09 '14 at 11:47
  • I am not clear about process flow between worksheets and DBs as per your last comments. Explain it further step by step and better to share some sample workbook to get a better understanding. – Nausherwan Aslam May 09 '14 at 11:53