Click here to Skip to main content
14,970,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a windows application where the user enters a specific task and it gets stored in a excel sheet as report in my documents folder.Now i want to add a functionality so that the user can specify the location where he wants to store the copy of the excel sheet.i.e.reading from the excel sheet in my documents and copying the contents to another excel sheet in the user defined location.I am able to read the contents of my documents excel sheet,but word by word.Can any1 plz help me to write the data into another excel sheet...?
Here is my code for reading excel sheet from my documents excel sheet.
Excell.Application xlApp;
Excell.Workbook xlWorkBook;
Excell.Worksheet xlWorkSheet;
Excell.Range range;

string str = "";
int rCnt = 0;
int cCnt = 0;

xlApp = new Excell.ApplicationClass();
object BlankValue = System.Reflection.Missing.Value;
string docpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string path1 = string.Format(docpath + @"\Report{0:yyyy_MM_dd}.xls",DateTime.Now.Date);
xlWorkBook = xlApp.Workbooks.Open(path1, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excell.Worksheet)xlWorkBook.Worksheets.get_Item(1);

range = xlWorkSheet.UsedRange;

for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
    for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
        str = (string)(range.Cells[rCnt, cCnt] as Excell.Range).Value2;


Using this,str is able to store only the last word in my sheet.How to add a List or Arraylist to store the entire data...?and how to copy all the data to another sheet..?is it by using streamwriter?help..
Updated 17-Aug-11 19:35pm

Read the excel cell by cell and sttore it in Arraylist/DataTable. and read it again. Here is good Link[^]
AC777 18-Aug-11 2:29am
i used list fr storing the data..and i m able to write it too...but nt able to save it...
           string docpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string path = string.Format(docpath + @"\Report{0:yyyy_MM_dd}.xls",DateTime.Now.Date);
string path2 = // User choosen path. Use File Browser to get the path
            // Create the file and clean up handles.
            using (FileStream fs = File.Create(path)) {}
            // Ensure that the target does not exist.
            // Copy the file.
            File.Copy(path, path2);
AC777 18-Aug-11 2:29am
senthil...thnx for ur reply..but I didnt get where to put tht...can u copy and paste d new code here..using my code and ur code..?tht wud b gr8...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900