I found the information on a blog. Your documentation needs to be more thorough and should require an additional link to a blog. I am now getting an error I need your insight to resolve. I cannot attach an .xlsx file to this forum so you need to give me some tools to get you the file that errors.
I use this code to import from a file:
Microsoft.Win32.SaveFileDialog dlg =
new
Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt =
".xlsx"
;
dlg.Filter =
"Excel | *.xlsx"
;
dlg.Title =
"Identify Location"
;
dlg.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
dlg.CheckFileExists =
false
;
dlg.CheckPathExists =
true
;
dlg.FilterIndex = 2;
dlg.RestoreDirectory =
true
;
bool
? dlgRst = dlg.ShowDialog();
if
(dlgRst ==
true
)
{
FileInfo file =
new
FileInfo(dlg.FileName);
XlsxFormatProvider provider =
new
XlsxFormatProvider();
using
(Stream stream =
new
FileStream(file.FullName, FileMode.CreateNew, FileAccess.Write))
{
provider.Export(reportExcelControl.Workbook, stream);
}
}
However, the import action corrupts the file... it can no longer be opened in Excel and its size changes to 0:
Telerik.Windows.Zip.InvalidDataException: Archive corrupted ---> System.IO.IOException: An attempt was made to move the file pointer before the beginning of the file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.SeekCore(Int64 offset, SeekOrigin origin)
at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
at Telerik.Windows.Zip.ZipArchive.ReadEndOfCentralDirectory()
--- End of inner exception stack trace ---
at Telerik.Windows.Zip.ZipArchive.ReadEndOfCentralDirectory()
at Telerik.Windows.Zip.ZipArchive.Init(Stream stream, ZipArchiveMode mode, Boolean leaveOpen)
at Telerik.Windows.Zip.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen, Encoding entryNameEncoding, CompressionSettings compressionSettings, EncryptionSettings encryptionSettings)
at Telerik.Windows.Documents.FormatProviders.OpenXml.OpenXmlImporter`1.Import(Stream input, IOpenXmlImportContext context)
at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider.ImportOverride(Stream input)
at Telerik.Windows.Documents.Spreadsheet.FormatProviders.WorkbookFormatProviderBase.Import(Stream input)
at Hpt.Product.Measurement.UC.MeasurementReportUC.btnImportFromFile_Click(Object sender, RoutedEventArgs e) in C:\HPT\Measurements\HPT.Product.Measurement.UC\MeasurementReportUC.xaml.cs:line 219
System.IO.IOException: An attempt was made to move the file pointer before the beginning of the file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.SeekCore(Int64 offset, SeekOrigin origin)
at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
at Telerik.Windows.Zip.ZipArchive.ReadEndOfCentralDirectory()
Thanks in advance for your help,
Joel