Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two strings

1 . string path // contains the path to which the file is saved

2 . string name // contains the name of the file which changes frequently

now i have to save the file with .xlsx format , how can i achieve this ???
:(( :(( :(( :((
----------------------------
what i have done is

xlWorkBook.SaveAs(path + "" +fName+".xlsx");

but am getting errors :(

please help me :)
Posted
Comments
OriginalGriff 30-Jan-11 6:04am    
Please don't post replies with "Add an Answer", use the "Add Comment" feature instead.
When add an answer, nobody is notified - only you could be.
When you add a comment, the person who wrote the answer gets an email. This should mean faster responses for you, as we aren't guaranteed to "check back" on our existing answers.
I have moved your answer to a comment, and deleted the answer.

BTW: You are welcome!

OK, now the correct answer:

C#
string fileName = string.Format(
    "{0}{1}{2}{3}",
    path,
    System.IO.Path.DirectorySeparatorChar, //important: '\' is incompatible!
    fName,
    ".xlsx");


Other answers does not take into account that the directory separator in the form of '\' is not portable — so I could not accept them. In particular, it won't work on Unix (yes, support of Unix is a part of CLI standard, in practice, this code can be used on Linux, Unix, MAC, etc. using Mono).

Also, string.Format is reportedly of better performance then a set of string concatenations (using '+').

Thank you for attention to these details.

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 31-Jan-11 15:22pm    
5+ for the portable answer :)
Try putting a '\' character between the path and the file name.

If that doesn't fix it, then we need to know the error message / exception detail.
 
Share this answer
 
Comments
OriginalGriff 30-Jan-11 6:01am    
The OP Wrote:
"Thank you guys , i got with answer
Improve Answer link | Posted 20 mins ago
phanindra_mangipudi452"
Espen Harlinn 30-Jan-11 6:29am    
5+ a happy "customer" :)
Sergey Alexandrovich Kryukov 30-Jan-11 22:35pm    
I would not be happy -- see my correction.
--SA
Sergey Alexandrovich Kryukov 30-Jan-11 22:34pm    
Griff, sorry, you have one incorrectness: '\' is not portable. Please see my answer.

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