0

I want to create the file with customer specified filename with specified extension

ex: sample.xlsx or sample.docx or sample.txt

and need to open it. i have used the below sample code and which is created and open the txt format files as expected but other format files such as (.xlsx,.doc) are created but which throws error

"file cannot open and specified file format and extension is invalid"

while trying to open the created file.

var hyperLink = new Hyperlink();
FileStream file = new FileStream(@Hyperlink.Address,
   FileMode.Create);                                 
hyperLink.NavigateUri = new Uri(file.Name);
file.Close();
if (hyperLink.NavigateUri != null)
    Process.Start(new ProcessStartInfo(hyperLink.NavigateUri.AbsoluteUri));
DrKoch
  • 9,556
  • 2
  • 34
  • 43
krish bala
  • 49
  • 8

3 Answers3

1

You can't open an empty *.docx or *.xlsx file, because such a file must contain valid contents.

Creating such contents is beyond the scope of this answer...

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • thanks,but it is possible to create the docx file with above code why not with xlsx? – krish bala Mar 27 '15 at 11:54
  • As I said: You need valid contents in any case. Probably zero bytes is (by accident) valid for .docx (but not useful anyway) – DrKoch Mar 27 '15 at 12:08
0

You can't create an empty docx and xlsx files as Drkoch told. You must use separate methods for creating xlsx and doc files.

For creating docx file, Please refer the below link: C#: Create and Manipulate Word Documents Programmatically Using DocX

For creating xlsx file, Please refer the below link: Create Excel (.XLS and .XLSX) file from C#

Community
  • 1
  • 1
sriman reddy
  • 763
  • 8
  • 22
0

You can use Open XML to create and manipulate office files without having MS Office installed on the server. https://msdn.microsoft.com/en-us/library/office/bb448854.aspx

sriman reddy is correct that you cannot just create Office files with FileStream. Office files are zip files containing specifically formatted files (mostly xml) that make up the document and it's contents.

Kevin
  • 157
  • 3