Click here to Skip to main content
14,923,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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.
C#
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..
Posted
Updated 17-Aug-11 19:35pm
v2

Read the excel cell by cell and sttore it in Arraylist/DataTable. and read it again. Here is good Link

http://stackoverflow.com/questions/5417088/transfering-data-from-one-excel-file-to-another[^]
   
Comments
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...
C#
           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
        try 
        {
            // Create the file and clean up handles.
            using (FileStream fs = File.Create(path)) {}
            // Ensure that the target does not exist.
            File.Delete(path2);
            // Copy the file.
            File.Copy(path, path2);
        } 
        catch 
        {
        
        }
}
   
Comments
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