Click here to Skip to main content
15,923,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to upload and save the the excel file in local storage .
and show on another window of WPF using document viewer.

in this we cannot use the Microsoft office. please avoid to use this in your solution.


please if anyone have the solution of this so, please tell me .
Posted
Updated 4-Sep-19 0:26am
v3
Comments
[no name] 28-Apr-14 7:31am    
Consult the documentation of whatever 3rd party library that you have chosen to use in place of the Excel interop libraries.

As far as i know DocumentViewer does not support MS Excel files. Please refer this link: How to open excel book with document viewer[^] to find out the solution.
 
Share this answer
 
Comments
M Kashif Saleem 29-Apr-14 7:40am    
thankx
Maciej Los 29-Apr-14 9:42am    
You're welcome.<br>
Please, mark this answer as a solution (green button) to remove your question from unanswered list.
XAML Code :

HTML
<grid>
        <textblock margin="104,10,436,25" fontweight="Bold">Upload Document File</textblock>
        <textbox height="32" horizontalalignment="Left" margin="21,35,0,0" name="FileNameTextBox">
                 VerticalAlignment="Top" Width="181" Text="
" />
        <button content="Browse" horizontalalignment="Left" margin="227,35,0,0">
                Name="btnBrowse" Width="88" Click="btnBrowse_Click" Height="32" VerticalAlignment="Top"/>
        <button content="view" horizontalalignment="Left" margin="355,35,0,0">
                Name="btnView" Width="88" Click="btnView_Click" Height="32" VerticalAlignment="Top"/>
        <documentviewer name="DocView" margin="21,76,32,30" />        
    </button></button></textbox></grid>


And Here it is .CS Code :

it is running.

C#
string filename = "";
        string xpsFileName = "";
        string path = "";

   private void ViewDocumentViewer()
        {
            try
            {
                var excelApp = new Microsoft.Office.Interop.Excel.Application();
                excelApp.DisplayAlerts = false;
                excelApp.Visible = false;
                Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path);
                ExportXPS(excelWorkbook);
                excelWorkbook.Close(false, null, null);
                excelApp.Quit();
                Marshal.ReleaseComObject(excelApp);
                excelApp = null;
            }
            catch
            { }
        }

        void ExportXPS(Microsoft.Office.Interop.Excel.Workbook excelWorkbook)
        {
            xpsFileName = (new DirectoryInfo(path)).FullName;
            xpsFileName = xpsFileName.Replace(new FileInfo(path).Extension, "") + ".xps";
            excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS,
            Filename: xpsFileName,
            OpenAfterPublish: false);
        }

        void DisplayXPSFile()
        {
            XpsDocument xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);
            FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();
            DocView.Document = fixedDocumentSequence;
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            ViewDocumentViewer();
            DisplayXPSFile();
        }

        private XpsDocument ConvertWordDocToXPSDoc(string wordDocName, string xpsDocName)
        {

            // Create a WordApplication and add Document to it

            Microsoft.Office.Interop.Word.Application

                wordApplication = new Microsoft.Office.Interop.Word.Application();

            wordApplication.Documents.Add(wordDocName);





            Document doc = wordApplication.ActiveDocument;

            // You must ensure you have Microsoft.Office.Interop.Word.Dll version 12.

            // Version 11 or previous versions do not have WdSaveFormat.wdFormatXPS option

            try
            {

                doc.SaveAs(xpsDocName, WdSaveFormat.wdFormatXPS);

                wordApplication.Quit();



                XpsDocument xpsDoc = new XpsDocument(xpsDocName, System.IO.FileAccess.Read);

                return xpsDoc;

            }

            catch (Exception exp)
            {

                string str = exp.Message;

            }

            return null;

        }

        private void BrowseFile()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



            // Set filter for file extension and default file extension

            dlg.DefaultExt = ".doc";

            dlg.Filter = "Word documents (.doc)|*.docx";



            // Display OpenFileDialog by calling ShowDialog method

            Nullable<bool> result = dlg.ShowDialog();



            // Get the selected file name and display in a TextBox

            if (result == true)
            {

                if (dlg.FileName.Length > 0)
                {

                    FileNameTextBox.Text = dlg.FileName;

                    string newXPSDocumentName = String.Concat(System.IO.Path.GetDirectoryName(dlg.FileName), "\\",

                                   System.IO.Path.GetFileNameWithoutExtension(dlg.FileName), ".xps");



                    // Set DocumentViewer.Document to XPS document

                    DocView.Document =

                        ConvertWordDocToXPSDoc(dlg.FileName, newXPSDocumentName).GetFixedDocumentSequence();

                }

            }
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {

            string filePath = "";
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".txt";
            dlg.Filter = "EXCEL Files (*.xls)|*.xlsx";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                filename = dlg.FileName;
                FileNameTextBox.Text = filename;
                filePath = System.IO.Path.Combine("C:\\Users\\m.kashif\\Desktop\\uploaded files\\");
            }
            SaveFileDialog _SD = new SaveFileDialog();
            _SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
            _SD.FileName = "Untitled";
            _SD.Title = "Save As";

            string directory = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            //  filePath = System.IO.Path.Combine(filename);

            string name = System.IO.Path.GetFileName(filename);
            filePath = "C:\\Users\\m.kashif\\Desktop\\uploaded files\\" + name;
            if (!File.Exists(filePath))
            {
                string destinationFilename = System.IO.Path.Combine("C:\\Users\\m.kashif\\Desktop\\uploaded files\\", name);
                File.Copy(filename, destinationFilename);
                path = destinationFilename;
                MKSEmployee._value = path;

            }
            else
            {
                MessageBox.Show("File Already Exist");
                FileNameTextBox.Text = string.Empty;
            }
        }</bool></bool>
 
Share this answer
 
v3
<><a href=""></a>
 
Share this 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