This dead-simple code creates a file that Excel won't open.
How could this be failing?
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(#"c:\dir\src.xlsx", true))
{
doc.SaveAs(#"c:\dir\saved.xlsx");
}
Notes:
Excel won't open saved.xlsx
src.xlsx is known to exist and be valid (Excel opens it no problem)
saved.xlsx is indeed produced, though it's about 500 bytes smaller than src.xlsx
If you meant this error:
Stop debugging before opening the "saved.xlsx" file
I've checked. It works correctly:
Output file
Related
I am generating several excel copies from a template (its really big).
For that First I am taking the template from a file location, then based on a loop for every iteration I am creating a new ExcelPackage(newFile,Template).
After that I am taking the exact ExcelWorksheet that I have to edit.
Then after editing I am Saving as the file as newFile. The time of opening the saved file Two problem is occurring:
If there is no Excel instance is running on the PC then the saved file is opening but with no data.
If the Excel instance is running then the saved file is opening with Warning message but working. "Problem with some content with Excel. Do you want us to recover?" and "Excel was able to recover some unreadable content "
string templateExcel = #"Location\template.xlsx";
FileInfo templateFile = new FileInfo(#"Location\newFile.xlsx");
using (FileStream templateExcelStream = File.OpenRead(templateExcel))
{
using (ExcelPackage copyExcel = new ExcelPackage(templateExcelStream))
{
ExcelWorksheet presentWorkSheet = copyExcel.Workbook.Worksheets["Name"];
presentWorkSheet.Cells[4, 2].Value = Value from condition;
copyExcel.SaveAs(templateFile);
}
}
Thanks all of you for your valuable time. I got the solution.
For me the issue was in the template itself as it contained invalid references to lookup tables. I found this in Formula -> Name Manager.
I suggest that you check the template if you face this issue.
How can we convert the excel file and word file to .pdf format from c#. i tried the following code but it shows the an error
this is my code:
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
wordDocument = appWord.Documents.Open(#"C:\Users\ITPro2\Documents\test.docx");
wordDocument.ExportAsFixedFormat(#"D:\desktop\DocTo.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
and i got the following Error
The export failed because this feature is not installed. during export to pdf from word from c#
While not directly related the documentation under
https://msdn.microsoft.com/en-us/library/office/ff198122.aspx
gives a note, that if the pdf add-in is not installed, exactly this error will occur. So check your prerequisites, i.e. Office installed and the add-in, too.
1) Excel 2013 Primary Interop Assembly Class Library and it works perfectly fine under .NET 4.5.1 Just add Microsoft.Office.Interop.Excel assembly to your references and you are ready to go.
using System;
using Microsoft.Office.Interop.Excel;
namespace officeInterop
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
Workbook wkb = app.Workbooks.Open("d:\\x.xlsx");
wkb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, "d:\\x.pdf");
}
}
}
OR
2) refer this link to convert DOC or DOCx file into PDF
http://www.rasteredge.com/how-to/csharp-imaging/pdf-convert-word-to-pdf/
Since my other comment got deleted, here is the updated version.
For converting my files to pdf in c# i used the metamorphosis libary, this could also be a solution for you.
Below is a code example from me where i used a blobstorage to download PDF files from regular files.
var converter = new SautinSoft.PdfMetamorphosis();
var ms = new MemoryStream();
await blob.DownloadToStreamAsync(ms);
ms.Seek(0, SeekOrigin.Begin);
var pdfStream = converter.DocxToPdfConvertStream(ms);
if (pdfStream != null)
{
await _storageProvider.SaveFileAsync(containerName, fileName, pdfStream);
}
ms.Close();
var result = await _storageProvider.GetFileWithAttributesAsync(containerName, fileName);
return new ServiceResponse<CloudBlockBlob>(result);
Below i posted some links with the sample code from the library itself:
Word to PDF example
Excel to PDF example
I have problem with this:
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true)) { }
Using just above and trying open document in Word showing error message that file is corrupted. It is interesting that for LibreOffice file is OK. I compared xml files (in docx) in WinMarge file before and after using this code and both are identical. Difference is only in size of docx file - why?
OK.. I resolved problem.. it's not nice solution but it's works..
var document = "template.docx";
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
// some editing stuff
wordDoc.Clone("ready.docx");
}
Now template.docx is corrupted, but ready.docx is fine.
I had the same problem, although your solution worked for me
wordDoc.Clone("ready.docx");
I found that in my case it was problem with letter encoding. I had the file generated from abby, image text to docx generation.
In order to check if encoding makes you trouble:
change youreditedword.docx to youreditedword.zip
open .zip, go to word folder, open document.xml
check document.xml' text that appears in word. If it is garbled, then you have encoding problem.
I fixed it this way - removed one phrase in my original unedited word file and writed it down again and saved it. Probably this way the encoding for the file is changed. Then after using openxml library and opening the file did not produce file is corrupted error
When I read the data from .xlsx file(ope in microsft excel) it genrates the error "File is used in another process". For data reading, I am using File.ReadAllBytes(strPath) Function.
So is there is any solution/code, which can remove this type of error so that I can succesfully read the data.
try this
using (FileStream fs = File.Open(filepath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
// do you stuf
}
I'm evaluating EPPlus as a replacement for GemBox, but can't even save a valid Excel file. I have this minimal C# code:
In library:
public Stream GenerateReport(string templateFilePath)
{
using (var xls = new OfficeOpenXml.ExcelPackage(new FileInfo(templateFilePath), true))
{
var stream = new MemoryStream();
xls.SaveAs(stream);
stream.Position = 0;
return stream;
}
}
In controller:
return File(GenerateReport("filename.xltx"), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "filename.xlsx");
The file is created and saved succesfully but when I open it with Excel I get the error:
"We found a problem with some content in 'filename.xlsx'. Do you want
to try to recover as much as we can?".
I answer Yes and get:
"The workbook cannot be opened or repaired by Microsoft Excel because
it is corrupt".
I have verified that the template file is in the correct format and none of the other suggested solutions here on stackoverflow fixes the problem.
Any suggestions?
UPDATE:
I also tried without using a template and that works without problem:
using (var xls = new OfficeOpenXml.ExcelPackage())
{
xls.Workbook.Worksheets.Add("New");
var stream = new MemoryStream();
xls.SaveAs(stream);
stream.Position = 0;
return stream;
}
UPDATE 2:
Saving the .xltx template as a .xlsx and using the .xlsx as the template does not raise any errors when opened.
I answered a similar question here:
.xlsx Created and Saved Using Template with EPPlus is Unreadable/Corrupt
Short answer: EPPlus does not actually support xltx template files, the 'template' parameter just expects an appropriately pre-formatted xlsx file.
I've recently found similar bug (Excel complained about some invalid xl/styles.xml section). After searching I came into solution: change version to 3.1.3 (3.1.2 has a bug). It seems 3.1.1 is ok too.