M Kashif Saleem
код XAML :
<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>
И вот он здесь .Код CS :
он работает.
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>