jexcel generating error on creating file - jexcelapi

InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
its now working this way
JExcel to write to Excel Worksheets with my Java Application.
How can I export data from an array to an Excel spreadsheet?

to write the output to a file
i think it may work in case of writing to the file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
// new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();

Related

ssis script task (vb) issues when reading large file

I'm using the below code inside a ssis script task to modify the contents of a file. I'm basicallly creating 1 json document when in the file there are many jsons, one after the other.
This code works perfectly up until around a 1GB file (to read the 1GB file it's using almost 7GB memory in SSIS), after that it crashes (i assume due to memory). I need to read files up until 5GB.
Any help please
Public Sub Main()
Dim filePath As String = Dts.Variables("User::filepath").Value.ToString()
Dim content As String = File.ReadAllText(filePath).Replace("}", "},")
content = content.Substring(0, Len(content) - 1)
content = "{ ""query"" : [" + content + "] }"
File.WriteAllText(filePath, content)
Dts.TaskResult = ScriptResults.Success
End Sub
It is not recommended to use File.ReadAllText(filePath) to read big flat files because it will store all the content in memory. I think you should use a simple data flow task to transfer the data from this flat file to a new flat file, and you can do the transformation you need in a script component on each row.
Also you can read it line by line in a script using a StreamReader using and write it to a new file using a StreamWriter, when finished you can delete the first file, and rename the new one.
References
How to open a large text file in C#
File System Read Buffer Efficiency
c# - How to read a large (5GB) txt file in .NET?

Detecting an empty flat file with SSIS script task

I am looking for a script task which identifies a zero KB file in a folder and outputs the same in a mail or text file.
Thanks in advance. Let me know for any questions.
Something like this:
String FilePath = Dts.Variables["User::FilePath"].Value.ToString();
String strContents;
StreamReader sReader;
sReader = File.OpenText(FilePath);
strContents = sReader.ReadToEnd();
sReader.Close();
if (strContents.Length==0)
MessageBox.Show("Empty file");

Is there a way to import an image from excel to a PictureBox?

I am writing an application that works with Excel files. So far I have been using Gembox spreadsheet to work with excel files. However, I discovered using Gembox spreadsheet I can save pics to excel files, but not retrieve them. Anyone can recommend how to retrieve a pic from excel file? Thank you
Here is how you can retrieve an image from an Excel file with GemBox.Spreadsheet:
ExcelFile workbook = ExcelFile.Load("Sample.xlsx");
ExcelWorksheet worksheet = workbook.Worksheets.ActiveWorksheet;
// Select Picture element.
ExcelPicture picture = worksheet.Pictures[0];
// Import to PictureBox control.
this.pictureBox1.Image = Image.FromStream(picture.PictureStream);
// Or write to file.
File.WriteAllBytes("Sample.png", picture.PictureStream.ToArray());

Apply VBA code to Excel file from SSIS

Good evening everyone.
I have to build a SSIS package that does as follows:
1) Execute a VBA code to a XLS file (Transpose a range into another range)
2) Save the XLS (In the same file or as a new file)
3) Import the modified XLS from the Transposed range.
Basically I have to transpose the data inside a XLS that I must import, and I didn't find a good way to do that in SSIS (Since the column range can change between files)
With this simple VBA script I can do that and make SSIS read the data in a very straightforward way. However I'm not finding a way to apply this code without modifying the Excel previously manually to add the script and run the VBA script. I want to automate this so the package prepares the xls, extracts the new data, and save it to a table.
Can anyone shed some ideas on how to apply this code or other ways to do this? The most important point I think is that it's a very specific range that I want to transpose.
Sub myTranspose()
With Range("a18:ZZ27", Range("a18:ZZ27").End(xlDown))
.Copy
Range("a30").PasteSpecial Transpose:=True
End With
End Sub
Create a Script Task that is piped into a Data Flow task
Edit the Script Task by double clicking the Script Task and clicking the Edit Script button.
Add references to Excel and CSharp as seen in this answer
Add some code similar to the following:
public void Main()
{
string filepath = #"c:\temp\transpose.xlsx";
Excel.Application xlApp;
Excel._Workbook oWB;
try
{
xlApp = new Excel.Application();
xlApp.Visible = false;
oWB = (Excel.Workbook)xlApp.Workbooks.Open(filepath);
Excel.Range fromrng = xlApp.get_Range("B4", "F5");
Object[,] transposedRange = (Object[,])xlApp.WorksheetFunction.Transpose(fromrng);
Excel.Range to_rng = xlApp.get_Range("A8", "A8");
to_rng = to_rng.Resize[transposedRange.GetUpperBound(0), transposedRange.GetUpperBound(1)];
to_rng.Value = transposedRange;
xlApp.ActiveWorkbook.Save();
oWB.Close(filepath);
}
catch (Exception ex)
{
//do something
}
Dts.TaskResult = (int)ScriptResults.Success;
}
This gives the following result in the sample transpose.xlsx I created.

Saving Files From Folder Into SQL Database In Windows Forms VB EF

I'm really struggling with this and any help would be greatly appreciated.
I need a bit of code that goes through each file in a folder and saves it into a database so that it can later be pulled out and displayed. I haven't got to the displaying part yet lol, still trying to get the files in the database.
What I have so far is giving me an error
Value of type '1-dimensional array of Byte' cannot be converted to 'String'.
All the fields in the database are nvarchar(MAX)
Dim dir As New System.IO.DirectoryInfo("C:\Users\Will\Desktop\SUBARU")
For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
Using db As New FileStoreAppDBEntities
Dim NewFile As New StoredFile
NewFile.FileName = f.Name
NewFile.FileSize = f.Length
'got trouble here
Dim ImageData As Byte() = System.IO.File.ReadAllBytes(f.FullName)
NewFile.FileContent = ImageData
'
NewFile.FileType = f.Extension
db.StoredFiles.AddObject(NewFile)
db.SaveChanges()
End Using
Next
Perhaps i'm doing this all wrong?
Many thanks for your help.
--EDIT
This seems to do it!
For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
Using db As New FileStoreAppDBEntities
Dim NewFile As New StoredFile
NewFile.FileName = f.Name
NewFile.FileSize = f.Length
Dim filename As String = f.FullName
NewFile.FileContent = System.IO.File.ReadAllBytes(filename)
NewFile.FileType = f.Extension.ToLower
db.StoredFiles.AddObject(NewFile)
db.SaveChanges()
End Using
Next
Not sure on the performance though, i think this might put the file in memory then send it instead of streaming?
And to retrieve the file:
Using db As New FileStoreAppDBEntities
Dim IDofFile As Integer = 31
Dim getFile = (From files In db.StoredFiles Where files.FileID = IDofFile Select files).SingleOrDefault
'getFile.FileName eg. mydocument.txt
System.IO.File.WriteAllBytes("C:\LocationToSaveTo\" & getFile.FileName, getFile.FileContent)
End Using
Getting an error for very large files System.OutOfMemoryException
Working perfectly for the smaller files though...
Perhaps chunking the file up would be possible?
Are your files text files? If so you should not read them as binary data but as text data. Something like this (sorry it's C# but it should be easy to convert to VB.NET):
using(StreamReader textFile = new StreamReader(path))
{
NewFile.FileContent = textFile.ReadToEnd();
}
If the files are binary files then you don't want to store them as strings in the database but probably as varbinary.
This seems to do it!
For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
Using db As New FileStoreAppDBEntities
Dim NewFile As New StoredFile
NewFile.FileName = f.Name
NewFile.FileSize = f.Length
Dim filename As String = f.FullName
NewFile.FileContent = System.IO.File.ReadAllBytes(filename)
NewFile.FileType = f.Extension.ToLower
db.StoredFiles.AddObject(NewFile)
db.SaveChanges()
End Using
Next
Not sure on the performance though, i think this might put the file in memory then send it instead of streaming?
And to retrieve the file:
Using db As New FileStoreAppDBEntities
Dim IDofFile As Integer = 31
Dim getFile = (From files In db.StoredFiles Where files.FileID = IDofFile Select files).SingleOrDefault
'getFile.FileName eg. mydocument.txt
System.IO.File.WriteAllBytes("C:\LocationToSaveTo\" & getFile.FileName, getFile.FileContent)
End Using
Getting an error for very large files System.OutOfMemoryException Working perfectly for the smaller files though...

Resources