Often companies using Excel to work on different data and information. Sometimes these data is exported from a external system like SAP or other line of business applications. A way to work together on those data is using office webapps or a normal SharePoint list. A SharePoint list has the advantage that you can add some more columns for some notes or status notes. This was one of the request at a customer who wants to import data from a Excel file in a monthly intervall.
For a small demo ho it works we used a Powershell Script for that, which imports the data. But first we save the Excel as csv.
The first problem was, that the special characters in german (ä,ö,ü) were not transformed correctly. So we did a small trick in Powershell. First we get the content and saved this content in another csv file with encoding to utf8.
After that we get this new csv file and read it into a Dataset with Encoding to unicode. Now we have the dataset and can iterate through the data, add or update the data or whatever you like to do with the data.
[sourcecode language=”csharp”]
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
############## PARAMETERS ####################
$WebUrl = “sitecollectionurl”
$Listname = “Listname”
#Convert CSV to UTF-8 with special characters
Get-Content D:\Folder\myCSV.csv | Out-File D:\Folder\myCSV_utf8.csv -Encoding utf8
#Get the CSV file and connect to the SharePoint list
$DataSet = import-csv -Delimiter “;” -Path “D:\Folder\myCSV_utf8.csv” -Encoding Unicode
######### Add to SPList ########
$spWeb = Get-SPWeb -identity $WebUrl # Get SPWeb
$list = $spWeb.Lists[$Listname] # Get SPList
for($i = 0; $i -le $DataSet.Count; $i++)
{
#Read current row
$row = $DataSet[$i]
$item = $list.Items.Add();
$item[“SPColumn1”] = $row.’Column1′
$item[“SPColumn2”] = $row.’Column2′
$item[“SPColumn3”] = $row.’Column3′
$item[“SPColumn_n”] = $row.’Column_n’
$item.Update()
}
[/sourcecode]
Hope this helps you.
Sorry but this doesn’t work.
Only a bunch of red for everything that is in the CSV file
This works great, thanks 🙂
Nice trick, thanks a lot!
great article. thanks
Nice trick. Thanks.
Great Help!! Thanks a lot.