Using Winnovative's excel library I am able to parse an excel file (xls) that contains a hyperlink. It works when the program is run directly on my machine. The exact same file will fail if I try to upload it to a website and parse it. Here is an example of the logic:
// Works when bytes are parsed from a program running locally.
// Fails when bytes are parsed from a file uploaded to a website
public void TestRead(byte[] bytes)
{
try
{
// Parse excel file
var workBook = new ExcelWorkbook(new MemoryStream(bytes));
// Save file
var toSave = workBook.Save();
System.IO.File.WriteAllBytes(#"C:\Users\[User]\Downloads\Test.xls", toSave);
}
catch (Exception e)
{
}
}
This is only an issue with xls files. I don't have trouble parsing a xlsx file with a hyperlink. The uploaded file fails to parse with an exception (which also crashes the application even though it is enclosed in a try-catch block):
Could not open the workbook.Arithmetic operation resulted in an overflow.
at Winnovative.ExcelLib.ExcelWorkbook..ctor(Stream excelStream, String openPassword, ExcelWorkbookDefaultSettings defaultSettings)
at Winnovative.ExcelLib.ExcelWorkbook..ctor(Stream excelStream)
and
System.OverflowException was unhandled
Message: An unhandled exception of type 'System.OverflowException' occurred in wnvxls.dll
Additional information: Arithmetic operation resulted in an overflow.
Related
I am trying to call in ISO Message from Website (ASP.NET). I imported all DLL and code. But while reading XML from GenericPackager class, file can't read exception is coming.
The same code is working fine in C# Windows application.
Here is the code:
IsoXML_Path = HttpContext.Current.Server.MapPath("~") + "PostCardFields.xml";
GenericPackager packager = new GenericPackager();
packager.readFile(IsoXML_Path);
ISOMsg isoMsg = new ISOMsg();
isoMsg.setPackager(packager);
Error in Exception:
Error reading (PATH)\PostCardFields.xml
(org.xml.sax.SAXException: SAX2 driver class org.apache.crimson.parser.XMLReaderImpl not found
java.lang.ClassNotFoundException: org.apache.crimson.parser.XMLReaderImpl)
(PATH) is the path of my XML file in directory. File is exists on that path.
I need to parse a csv file and import it into a oracle database table. I use the Lumenworks Framework with this code:
using (CsvReader csv = new CsvReader(new StreamReader(sFile), true))
{
Console.WriteLine("test3");
}
But if I run the code, the following exception appears:
Application: Application.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
Stack:
at Application.Program.Main(System.String[])
But the weird thing is, if I only execute the new Streamreader(sFile) part and write this on the console, no exception appears. I already debugged the sFile and this is a valid path.
If you have a new StreamReader(sFIle); and the file does not exists it will throw an exception. The path could be a valid formatted path, but if the file is not there then the exception thrown, FileNotFoundException, would make perfect sense.
Check to make sure that the file exists at the specified path before trying to open the stream.
if (File.Exists(sFIle) {
using (CsvReader csv = new CsvReader(new StreamReader(sFile), true)) {
Console.WriteLine("test3");
}
}
What a mistake. After hours I realized that the Lumenworks.dll wasn't copied to the application.exe..
Another exception than System.IO.FileNotFoundException would be truly grateful.
I am using C# to create a function to write data to a file in File System, and reate it and repeat function if file doesn't exist.
The first method of just writing the data is working. But the second method, if the file is not present and the program has to first create it, isn't working. It creates the file but at the very same time throws an exception to me in Visual Studio saying
System.NotSupportedException is unhandled
I am using the exact copy of code that the MSDN is having.
http://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx
Here is what I am using in my second code block,
// Create file!
using (FileStream fs = File.Create(file))
{
Byte[] info = new UTF8Encoding(true).GetBytes("some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Continue again with the request.
createFile(file);
The method declaration (if required) is as
private static void createFile (string fileName) {
string file = "C:\\Users\\AfzaalAhmad\\Documents\\" + fileName + ".txt";
/* two methods here */
}
The Image is as: (Note that there is no error in the path of file) I have used
Console.Write(file); // to get the path, and its OK!
See it in the image below ↓
Please note, that it does create the file in the Documents folder. But throws this exception. What am I doing wrong here?
Note the detail in the exception report: "The given path's format is not supported."
Also look at the contents of your variable file - it appears to be #"C:\Users\AfzaalAhmad\Documents\C:\Users..." - i.e. it contains the path twice.
So even though the operating system might have managed somehow to create some sort of file, the filename does not contain a valid value.
[edit] createFile(file); and Console.Write(file); are both taking the value #"C:\Users\AfzaalAhmad\Documents\dsg b.txt" but your method createFile is then adding the path a second time. Change it to simply:
private static void createFile (string file) {
/* two methods here */
}
Please have a exact look at the Exception message and the value of your file variable! There is your error!
I'm attempting to make changes to a customer-supplied .xlsm file (a C# application will fill with data from a web-form and submit to a CMS) using EPPlus.
But on package.Save(name) -- or packagae.SaveAs(name) -- an exception is thrown by the DataValidations collection:
2011-11-28 14:17:51,380 [9] ERROR app.ExcelConverter - System.ArgumentException: Requested value 'date' was not found.
at System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)
at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at OfficeOpenXml.DataValidation.ExcelDataValidationCollection..ctor(ExcelWorksheet worksheet)
at OfficeOpenXml.ExcelWorksheet.get_DataValidations()
at FItoExcelCore.ExcelConverter.RunSample(String filePath) in \path\to\class.cs:line 32
Even attempting worksheet.DataValidations.Clear() throws the same exception, and this is without any changes to the file:
try
{
using (ExcelPackage package = new ExcelPackage(output))
{
package.SaveAs(output);
}
}
catch (Exception ex)
{
_log.Error(ex.ToString());
}
I do not have this behavior in an excel file that I create from scratch, but I need to use the supplied file.
Don't know if it's significant, but I'm working in the .Net 4 Framework, and it's an 1.01 MB .xlsm file. The file saves/saves-as fine (even with new data) from within Excel.
NOTE: apparently .xlsx files work just fine, but the the .xlsm format contain VBA macros that can't be saved in the .xlsx format, so the file-type cannot be converted.
As usual, within sixty seconds of posting I find new information online.
There is a discussion at the EPPlus codeplex site that suggests that "there is no real support for [xlsm files] in the component."
The may be the source of the problem.
Do you have the latest version of EPPlus?
They fixed this issue March of 2011. It had to do with ValidationTypes being case insensitive.
See:
http://epplus.codeplex.com/workitem/13246
Edit:
You can also try saving it as .xlsx if xlsm isn't supported.
As Ed Wrote i have donwloaded the last version of EPPlus which is from Jan 31 2012,and it worked very well with its validation. The excel im reading and writting is a .xlsx format.
Can anybody share c# code to validate whether the input excel file is the real excel file or an xmlexcel file(file generated by using xmlstream)
i want this because the user uploads his excel file and am using Microsoft.ACE.OLEDB.12.0 to import it into database after import the duplicates records(available in the uploaded excel file) were dumped to an excel(thru xmlstream) and given to the user as a download link ..the user then uses this file to upload again...but the Microsoft.ACE.OLEDB.12.0 doesn't recognizes it..so am using Extended Properties=HTML Import but i can't find a way to retrive the data from the file(as it is an xml based excel file)
Use the magic number signature from the following URL:
http://www.garykessler.net/library/file_sigs.html
You can use the values specified there to recognise if the file is a binary XLS.
You can validate file type by adding nested try..catch.. clause.
try
{
//open connnection for x file
}catch(Exception ex)
{
try
{
//open connection for y file
}catch(...)
{
}
}