I would like to output sql output to an excel file, and a sheet that I give a name to, i.e. not "Sheet1". How do I even begin code this?
Below is current code that reads sql output
$sql_output = #()
while ($rdr.read()){
$sql_output += [PSCustomObject][Ordered]#{
Col1=$rdr.GetValue(0)
Col2=$rdr.GetValue(1)
Col3=$rdr.GetValue(2)
Col4=$rdr.GetValue(3)
Col5=$rdr.GetValue(4)
}
$count=$count + 1
}
Then exports to csv
$sql_output | Export-CSV "D:\Script\Network_Threat_Protection.csv" -NoTypeInfo -Append
I would eventually like this powershell script to read multiple sql queries and output it to different sheets within excel, but let me get the jist of exporting to excel first ....
Export-CSV does not generate an Excel file. It generates a comma delimited text file that usually is set to open with Excel. Being a CSV file there is no knowledge of multiple sheets or dataset properties like sheetnames, for that you will need to use the Excel comobject (and all of its nuances).
Here is a basic example of writing a real Excel file:
$a = New-Object -com Excel.Application
$a.Visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.Cells.Item(1,1) = "A value in cell A1."
$b.SaveAs("C:\Scripts\Test.xls")
$a.Quit()
What you are trying to do is not new. There are a lot of scripts on the internet for this task. Microsoft's Script Center is a good place to find powershell scripts. Here is one that seems to do what you want.
you can use CPPLUS library for exporting tables to excel sheets..
private DataTable GetData(string tableName)
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
SqlCommand sqlCommand = new SqlCommand("SELECT * FROM " + tableName, sqlCon);
sqlCon.Open();
var reader = sqlCommand.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}
}
private void SaveExcel(DataTable dataTable, string newFile)
{
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(newFile);
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
pck.SaveAs( new System.IO.FileInfo("d:\\Export\\" + newFile + ".xlsx"));
}
}
Download Source
Related
I've found this answer in C (Or C# maybe), but I don't really understand C. I'm a vb guy (and new to .net). Can anyone give me a hand in converting this to vb?
The concept appears to be exactly what I need:
I've got an Excel worksheet loaded to a dataset table.
I have an identical table in the SQLite db (column for column).
I want to save the imported Worksheet data in the dataset table to the empty SQLite table.
Here is the code from the link:
SQLiteConnection m_dbConnection;
void createDbAndTable()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite; Version=3;");
m_dbConnection.Open();
string sql = "create table myValues (name varchar(20), highScore int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
void fillTable(DataSet ds)
{
var dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
var name = dr["name"].ToString();
var score = Convert.ToInt32(dr["value"].ToString());
string sql = "insert into myValues (name, highScore) values ( '" + name + "'," + score + ")";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
m_dbConnection.Close();
}
Any help would be greatly appreciated!
My question is regarding sql server bcp utility:
How do I export data from sql server into excel sheet with the following extensions e.g. xls,xlsx, and xlsb using bcp tool, because I can export to .csv file and .txt file extensions but with excel it can not be open after exporting with bcp.
Any worker around to export into excel sheet will do help alot.
Thanks for your help in advance
First of all there BCP don't support xls or xlsx formats.
BCP only support xml,txt and csv;
If you have to import data from excel you have .net or java or Php to create a data table with the same excel. Then use that datatable to create corresponding table in Sql
if you are using sql and C# then this might help you
string con =
#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;" +
#"Extended Properties='Excel 8.0;HDR=Yes;'";
using(OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using(OleDbDataReader dr = command.ExecuteReader())
{
while(dr.Read())
{
var row1Col0 = dr[0];
Console.WriteLine(row1Col0);
}
}
}
or
private void GetExcel()
{
string fullPathToExcel = "<Path to Excel file>"; //ie C:\Temp\YourExcel.xls
string connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=yes'", fullPathToExcel);
DataTable dt = Function_Library.DatabaseFunctions.GetDataTable("SELECT * from [SheetName$]", connString);
}
I have used a variety of methods to try and import this data (a 97-2000 xls file generated by a crystal report server each morning that fills the first sheet fully and more than half fills the second sheet - about 120,000 records in total although it varies). There are about fourteen columns in the data, but the problem is that the first and second row have some title data taking up two columns that cannot be stopped from being entered by the report server. When I go to import the data with code such as:
Imports System.Data.OleDb
Public Class ImportData
Private Const databaselocation As String = "G:\db.mdb"
Private Const excelfileloc As String = "D:\"
Private connstr As String = "Provider=Microsoft.JET.OLEDB.4.0; Data Source=" & databaselocation & "; User Id=admin; Password=;"
Private excelstring1 As String = "Provider=Microsoft.JET.OLEDB.4.0; Data Source=" & excelfileloc
Private excelstring2 As String = "; Extended Properties=""Excel 8.0; HDR=NO; IMEX=1"";"
Public Function ImportExcel() As Boolean
Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection(connstr)
Dim command As New OleDb.OleDbCommand
connection.Open()
command.Connection = connection
Try
Using conn As New OleDbConnection(excelstring1 & "excel.xls" & excelstring2)
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO [MS Access;Database=" & databaselocation & "].[dbo_impExcel] SELECT * FROM [Sheet1$]"
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Catch ex As Exception
End Try
End Function
End Class
It only imports the first two lines and ignores the rest. When I then repeat the code but using [Sheet2$], the data is returned fine. If I manually go into the excel file and delete the title, the data is imported fine. At the moment I am using code to delete the title lines first, but I'm sure there should be a better way.
In my experimentations I have even tried creating a stored procedure in an MS SQL database using OPENROWSET and the problem still occurs (although due to various job constraints this job must only rely on JET.
You might try using VB in an Excel worksheet to create and copy proper data to a new xls file. Personally I tend to save things as tab delimit or Pipe text file instead of importing as xls. Saves me the frustration of trying to Import xls.
If the file has the same title every day or based on date you should be able to just code it in a xls file which does all the work for you then closes afterward.
I have a plain text file that contains one column of numbers, and in a web client take the numbers and transfer them into a SQL Database. After retrieving the file, I store the contents in a temporary DataTable, then using SqlBulkCopy, attempt to transfer the DataTable to the database. However, once I run the web client and import the file, the program hangs. The code is listed below for what I've done so far.
Transferring Files to DataTable
private DataTable readTextFile()
{
DataTable dt = new DataTable();
FileUpload file = new FileUpload();
dt.Columns.Add("Claim Number", typeof(Int32));
file = (FileUpload)grdCriteria.FindControl("exportUpload");
StreamReader read = new StreamReader(file.PostedFile.FileName);
while ((read.ReadLine()) != null)
dt.Rows.Add((Int32.Parse(read.ReadLine())));
return dt;
}
Insert DataTable into SQL Database
DataTable dt = readTextFile();
SqlBulkCopy bk = new SqlBulkCopy(Profile.ConnectionKey.CAM);
bk.DestinationTableName = dt.TableName;
bk.WriteToServer(dt);
Thanks!
I am connecting to an Excel spreadsheet via OleDbConnection .
How do I order by ColumnNumber ? I'd like to do something like :
SELECT * FROM [Sheet1$] ORDERBY ColumnNumber
where ColumnNumber is a number like 1 or 2 ?
Any ideas ?
Note: the file I'm trying to open has no headers.
private String BuildConnectionStringXLS()
{
String fileName = GetFileName();
Dictionary<string, string> props = new Dictionary<string, string>();
props["Provider"] = "Microsoft.Jet.OLEDB.4.0";
props["Data Source"] = fileName;
props["Extended Properties"] = "\"Excel 8.0;HDR=No;IMEX=1\"";
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> prop in props)
{
sb.Append(prop.Key);
sb.Append('=');
sb.Append(prop.Value);
sb.Append(';');
}
return sb.ToString();
}
public DataTable GetFullTable(int columnToOrderBy)
{
String fileName = GetFileName();
DataTable resultDataTable = new DataTable();
String connectionString = BuildConnectionString();
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$] ORDERBY ColumnNumber", conn);
da.Fill(resultDataTable);
conn.Close();
return resultDataTable;
}
I find it surprising that you are calling [sheet1$] directly without referring to any linked server.
Here are 2 approaches that are possible:
Using Linked Server
First create a linked server to access excel data as below:
(I am assuming you have already have either JET or ACE provider for excel)
exec sp_addLinkedServer #server='ExcelLnkdServr',
#srvproduct='ACE 12.0',
#provider='Microsoft.ACE.OLEDB.12.0',
#datasrc='\\sysdev\loadExcel.xlsx',
#provstr='Excel 12.0;HDR=Yes';
/* If the above creation is successful, you can see a listing when use SP_LINKEDSERVERS in SSMS. Now you can query the sheet using order by as below: */
select * from ExcelLnkdServr...[Sheet1$]
order by 1 asc, 2 desc
Using Adhoc Queries
You can OPENROWSET, OPENDATASOURCE or OPENQUERY as shown below: (Second option doesnt need any linked server creation )
select * from openquery(ExcelLnkdServr, 'SELECT * FROM [Sheet1$] order by 1 asc, 3 desc')
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=\\sysdev\loadExcel.xlsx;HDR=Yes','Select * from [Sheet1$] order by 1 asc')