Managing links in Excel source workbooks when duplicating source files - database

I have three files:
Activefile - where my code is stored and run
Databasefile - where my raw data is housed (has lots of protection)
Copyofdatabasefile - is a copy without the protection
I have a macro that runs in activefile to update a databasefile excel file, later in the macro, I then use the saveas method on the databasefile to make a copyofdatabasefile file, I remove some functionality to allow people to access the data easily without going through some of the checks on the main databasefile.
When saving the copyofdatabasefile, the links in my active file are updated to look at the new copyofdatabasefile file. I don't want this to happen.
How can I adjust my excel links/code to ensure that the links in my file aren't transferred across to the copyofdatabasefile?
Saveas macro options are currently:
Databasefile.SaveAs filename:="\\somelocation\copyofdatabasefile.xlsx", FileFormat:=51, CreateBackup:=False

Using the Workbook.SaveCopyAs Method should work.
If your original file is xlsx use
Databasefile.SaveCopyAs Filename:="\\somelocation\copyofdatabasefile.xlsx"
Note that it saves in the same FileFormat as the original file only!
If your original file is xlsm use
If you need to change the file format (eg from xlsm to xlsx) you need to save as copy in the original file format first, then reopen that copy with Workbooks.Open() and then use .SaveAs to change the FileFormat.
Databasefile.SaveCopyAs Filename:="\\somelocation\copyofdatabasefile.xlsm" 'if original file was xlsm
Dim wb As Workbook
Set wb = Workbooks.Open("\\somelocation\copyofdatabasefile.xlsm")
wb.SaveAs filename:="\\somelocation\copyofdatabasefile.xlsx", FileFormat:=51, CreateBackup:=False
wb.Close False
Kill "\\somelocation\copyofdatabasefile.xlsm" 'delete old format

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?

Alasql - How to get the sheet names from an xlsx file?

According to: https://github.com/agershun/alasql/wiki/XLSX you can send in the sheetid to read out a different sheet.
I can't however find any way to read out the available sheets.
Does anyone know how this should be done?
Instead of using alasql, first read your workbook yourself using the xlsx plugin
// file is instanceof File object
let workbook = XLSX.read(URL.createObjectURL(file));
console.log(workbook.SheetNames);

filter filename from folder that contains a specific text in ssis

I am trying to move some files from one folder to another, and i Need to evaluate if a file name contains certain text then only we need to move those files.
for example , i have following files in a folder.
abcd_takeme_fdsljker.txt
abcd_file_fdsljker.txt
abcd_takeme_fdsljsdfker.txt
abcd_filetk_fdsljker.txt
abcd_takeme_fdsljssker.txt
from the above I want to pick files which has text "takeme"
Using For each loop container
You have to add a for-each loop container to loop over files in a specific directory.
Choose the follow expression as a filename:
*takeme*
Map the filename to a variable
Add a dataflow task inside the for each loop to transfer files
use the filename variable as a source
you can follow the detailed article at:
http://www.sqlis.com/sqlis/post/Looping-over-files-with-the-Foreach-Loop.aspx
if you want to add multiple filter follow my answer at:
How to add multiple file extensions to the Files: input field in the Foreach loop container SSIS
Using a script task
or you can achieve this using a script task with a similar code: (i used VB.Net)
Public Sub Main()
For Each strFile As String In IO.Directory.GetFiles("C:\New Folder\", "*takeme*", IO.SearchOption.AllDirectories)
Dim filename As String = IO.Path.GetFileName(strFile)
IO.File.Copy(strFile, "D:\New Folder\" & filename)
Next
Dts.TaskResult = ScriptResults.Success
End Sub

VBA - file saves as tab-delimited instead of csv

I'm getting slightly confused by this issue. I have a macro that grabs data from one spreadsheet, re-formats it and saves in another spreadsheet. Everything works perfectly well but this piece of code seems to be working incorrectly:
Set NewBook = Workbooks.Add
With NewBook
.Title = "Pts"
.SaveAs Filename:="C:\Minestar_exports\" & Pts & "", FileFormat:=xlCSV,
CreateBackup:=False
.Close
End With
The trouble is that it save the file all right but seems to ignore the FileFormat:=xlCSV bit and saves it as TAB-delimeted instead. It's no biggie, when macro finishes running I just overwrite the temporary file using proper file format but still I couldn't figure out why this is happening. Any suggestions?
maybe, it has to do with your Windows regional settings.
Have a look to: http://excel.tips.net/T003232_Specifying_a_Delimiter_when_Saving_a_CSV_File_in_a_Macro.html
with regards, Christian Hahn.

Copy text from WPF DataGrid to Clipboard to Excel

I have WPF DataGrid (VS2010 C#). I copied the data from DataGrid to Clipboard and write it to an Excel file. Below is my code.
dataGrid1.SelectAllCells();
dataGrid1.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dataGrid1);
dataGrid1.UnselectAllCells();
string path1 = "C:\\test.xls";
string result1 = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
Clipboard.Clear();
System.IO.StreamWriter file1 = new System.IO.StreamWriter(path1);
file1.WriteLine(result1);
file1.Close();
Everything works out OK except when I open the excel file it gives me two warning:
"The file you are trying to open
'test.xls' is in a different format
than specified by the file extension.
Verify that the file is not corrupted
and is from a trusted source before
opening the file. Do you want to open
the file now?"
"Excel has detected that 'test.xls' is
a SYLK file, but cannot load it."
But after I click through it, it still open the excel file OK and data are formated as it supposed to be. But I can't find how to get rid of the two warnings before the excel file is open.
You need to use csv as extension. Xls is the Excel file extension.
So
string path1 = "C:\\test.csv";
should work.
A problem like yours has already been described here : generating/opening CSV from console - file is in wrong format error.
Does it helps to solve yours ?
Edit : Here is the Microsoft KB related => http://support.microsoft.com/kb/323626

Resources