Export Table from SQL Server to Excel 2007 using C# - c#

my table in sql looks like this:
CREATE TABLE InjuryScenario
(
InjuryScenario_id int identity(1,1),
InjuryDay int,
InjuryMonth int,
InjuryYear int,
InjuryDesc varchar(80),
InjuryComments varchar(50),
AlmostInjury int,
InjuryInSchool varchar(20),
ProductInjury varchar(20),
Cause_id int,
CauseType_id int,
CauseChar_id int,
Place_id int,
PlaceType_id int,
InjuryDate_id int,
Username varchar(50),
TimeStamp datetime default (getdate()),
constraint pk_InjuryScenario_id primary key (InjuryScenario_id),
constraint fk_Cause_InjuryScenario foreign key(Cause_id) references Cause(Cause_id) on delete cascade,
constraint fk_CauseType_InjuryScenario foreign key(CauseType_id) references CauseType(CauseType_id) on delete no action,
constraint fk_CauseChar_InjuryScenario foreign key(CauseChar_id) references CauseChar(CauseChar_id) on delete no action,
constraint fk_Place_InjuryScenario foreign key(Place_id) references Place(Place_id) on delete cascade,
constraint fk_PlaceType_InjuryScenario foreign key(PlaceType_id) references PlaceType(PlaceType_id) on delete no action,
constraint fk_InjuryDate_InjuryScenario foreign key(InjuryDate_id) references InjuryDate(InjuryDate_id) on delete cascade,
constraint fk_Users_InjuryScenario foreign key(Username) references Users(Username) on delete cascade
)
I fill this table with data and i want to show this table in Excel 2007.
how can i do it using c#?
Thank you!

protected void insertBTN(object sender, EventArgs e)
{string conString = #"Data Source =XXXX; Initial Catalog=XXXX; Persist Security Info=True;User ID=XXXX; Password=XXXX";SqlConnection sqlCon = new SqlConnection(conString);
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * from InjuryScenario", sqlCon);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 2; i++)
{
for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
{
if (i == 1)
{
ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
}
else
ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 2][j - 1].ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\Mor Shivek\\Desktop\\test.xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();}

This is hundred percent working for VS-2013 Premium for C#. just copy
and paste the code That's it. Its working for SQL Server database You
can save the data into xls, xlsx, or csv and can use that same csv
for parameterization. you need to install following packages to make
it work.
using System.Data.SqlClient
using Excel=Microsoft.Office.Interop.Excel
using SQL = System.Data
///***Copy from here////
SqlConnection cnn;
string connectionstring = null;
string sql = null;
string data = null;
int i = 0;
int j = 0;
////*** Preparing excel Application
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
///*** Opening Excel application
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(#"Fullpath\Book1.csv");
xlWorkSheet = (Excel.Worksheet)(xlWorkBook.ActiveSheet as Excel.Worksheet);
////*** It will always remove the prvious result from the CSV file so that we can get always the updated data
xlWorkSheet.UsedRange.Select();
xlWorkSheet.UsedRange.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
xlApp.DisplayAlerts = false;
//xlWorkBook.Save();
/////***Opening SQL Database using Windows Authentication
connectionstring = "Integrated Security = SSPI;Initial Catalog=DatabaseName; Data Source=ServerName;";
cnn = new SqlConnection(connectionstring);
cnn.Open();
////** Write your Sql Query here
sql = "SELECT TOP 10 [FirstName],[MiddleName],[LastName],[Email],[AltEmail],[AuthorizedUserName] From Tablename";
///*** Preparing to retrieve value from the database
SQL.DataTable dtable = new SQL.DataTable();
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
SQL.DataSet ds = new SQL.DataSet();
dscmd.Fill(dtable);
////*** Generating the column Names here
string[] colNames = new string[dtable.Columns.Count];
int col = 0;
foreach (SQL.DataColumn dc in dtable.Columns)
colNames[col++] = dc.ColumnName;
char lastColumn = (char)(65 + dtable.Columns.Count - 1);
xlWorkSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
xlWorkSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
xlWorkSheet.get_Range("A1", lastColumn + "1").VerticalAlignment
= Excel.XlVAlign.xlVAlignCenter;
/////*** Inserting the Column and Values into Excel file
for (i = 0 ; i <= dtable.Rows.Count - 1; i++)
{
for (j = 0; j <= dtable.Columns.Count-1; j++)
{
data = dtable.Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}
///**Saving the csv file without notification.
xlApp.DisplayAlerts = false;
xlWorkBook.Save();
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}

I tried this code it's working.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cnn;
string connectionstring = null;
string sql = null;
string data = null;
int i = 0;
int j = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
connectionstring ="Data Source=IN-WTS-SAM;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
cnn = new SqlConnection(connectionstring);
cnn.Open();
sql = "SELECT * FROM Emp";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
}
xlWorkBook.SaveAs("informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file D:\\Sam-informations.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
Try this if it works for you.

Export sql table to excel with column name
for (i = 0; i <= ds.Tables[0].Columns.Count - 1; i++)
{
data = ds.Tables[0].Columns[i].ColumnName.ToString();
xlWorkSheet.Cells[1, i + 1] = data;
}

Related

export .db table to excel with header/column and values

Hi All: Uisng SQLiteDataReader i'm getting the table values but i'm not getting the column value using SQLiteDataReader GetName
My goal is to i want data with column name in excel file which is same as query then followed by the table values. wWhat is the mistake here? thanks
I think i'm making mistake in the for loop?
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = xlWorkBook.Worksheets[1]; // (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Stats";
string cs = #"C:\\db_test\\Database.db";
string data = String.Empty;
int i = 0;
int j = 0;
using (SQLiteConnection con = new SQLiteConnection("Data Source=C:\\db_test\\Database.db"))
{
con.Open();
//string stm = "SELECT Oid from Stats";
StringBuilder query = new StringBuilder();
query.Append("SELECT");
query.Append("[Oid], [CreatedOn], [OrderOid] ");
query.Append(",[OrderLineOid], [OrderNumber], [Product] ");
query.Append(",[PaperTypeName], [OutputProfile], [OutputProfileChannel] ");
query.Append("FROM Stats");
using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read()) // Reading Rows
{
var columns = new List<string>();
for (j = 0; j <= rdr.FieldCount - 1; j++) // Looping throw colums
{
columns.Add(rdr.GetName(j));
data = rdr.GetValue(j).ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
i++;
}
}
}
con.Close();
}
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +"\\"+ "Statictics_" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
It looks like you likely got the solution you needed from the comments on your post, but I'll offer another. I maintain a library, Sylvan.Data.Excel that will be easier and much faster than Excel interop. The library is open source and has a permissive license (MIT).
Writing to an .xlsx file only takes a couple lines of code.
SQLiteDataReader rdr = GetData(); // get your data reader from Sqlite
using var writer = ExcelDataWriter.Create("mydata.xlsx");
writer.Write(rdr, "Stats");
While my library supports reading most common Excel formats, it only supports writing .xlsx files. It is the fastest Excel library for .NET. For comparison, using Excel interop (the code your provided above) takes ~15 minutes to write the 65k records that Sylvan can write in less than half a second. My library also doesn't require that Excel be installed on the machine, its only dependency is the core .NET runtime libraries.

C# memory leak using exel and interop COM

UPDATED 3/12/20. I am struggling with a problem where the Excel application is not releasing following my function call below. I'm not sure what references I'm screwing up but I assume I'm somehow leaving a reference to the application because I see multiple Excel processes in my task manager as the program runs, not to mention large amounts of memory use that don't improve with GC. Thoughts?
static void ParseExcel(string file, SqlConnection conn)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(file);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range rng = xlWorksheet.UsedRange;
SqlCommand cmd = conn.CreateCommand();
double colCount = rng.Columns.Count;
double rowCount = rng.Rows.Count;
//iterate over the rows and columns
//start on row 2 since row 1 is column headers
for (int i = 2; i <= rowCount; i++)
{
string companyName = "";
for (int j = 1; j <= colCount; j++)
{
//write the value to the console
if (rng.Cells[i, j] != null && rng.Cells[i, j].Value2 != null)
{
string tst = rng.Cells[i, j].Value2.ToString();
switch (j)
{
case 2:
companyName = tst;
break;
}
} //end of column
}//end of row
//write to database
string sql = "insert into dbo.company( ";
sql += DBI(companyName) + ")";
Console.WriteLine("sql = " + sql);
try
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
xlApp.Quit();
Marshal.ReleaseComObject(rng);
Marshal.ReleaseComObject(xlWorksheet);
Marshal.ReleaseComObject(xlWorkbook);
Marshal.ReleaseComObject(xlApp);
System.Threading.Thread.Sleep(3000);
}
and outside call looks like this:
ParseExcel(#"c:\testfile.xls", conn);
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
I should not have made my parse method static. That was the cause of the memory leak.

Write data to Excel - C#

I need to write data to an excel sheet and need to open it after writing it. This is the code I am using..
object misValue = System.Reflection.Missing.Value;
Excel.Application xlAppEnv = new Excel.ApplicationClass();
Excel.Workbook xlForEnv = xlAppEnv.Workbooks.Add(misValue);
Excel.Worksheet xlForEnv_View = (Excel.Worksheet)xlForEnv.Worksheets.get_Item(1);
xlForEnv_View.Name = "PF Keys";
xlForEnv_View.Cells[row, column] = "data";
I could write data using the above code and when I am done, I could save the file to a predefined location using the below code..
envSaveLoc = envSaveLoc + "\\PF Keys.xlsx";
xlForEnv.SaveAs(envSaveLoc, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlAppEnv.DisplayAlerts = true;
xlForEnv.Close(true, misValue, misValue);
xlAppEnv.Quit();
The above code is working fine but now the requirement is that the program shouldn't save it but once the data is written to the excel sheet, open the file in excel and present to the user. User can then review and save it himself by using File->Save as option. How can I achieve it? The data can be stored in some temporary location for presenting to the user.
What about simply showing the Excel application window using
xlAppEnv.Visible = true;
This should display the Excel window with all the data you've written and let the user decide how to proceed.
This is what I use to run a SQL query and house the returned results in a data set then write the dataset to Excel. From there you can do whatever you wish with it, save/print/convertoPDF etc etc
int i = 0;
int j = 0;
_Workbook workbooksExcel;
Worksheet worksheet;
Excel._Application docExcel;
//This will run a SQL Query and write results to DataSet
SqlConnection conn;
string sql = null;
object misValue = System.Reflection.Missing.Value;
docExcel = null;
string ConnectionString = "Data Source=DS;Initial Catalog=TestDB;User ID = sa;Integrated Security=True;MultipleActiveResultSets=True";
conn = new System.Data.SqlClient.SqlConnection(ConnectionString);
conn.Open();
sql = "Insert SQL Statement Here";
System.Data.SqlClient.SqlDataAdapter dscmd = new System.Data.SqlClient.SqlDataAdapter(sql, conn);
ds = new DataSet();
dscmd.Fill(ds);
conn.Close();
//Here is where the above SQL Statement will write to Excel
for (i = 0; i <= SQLConnection.ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= SQLConnection.ds.Tables[0].Columns.Count - 1; j++)
{
try
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
_Workbook workbooksExcel = docExcel.ActiveWorkbook;
Worksheet sheetExcel = (Worksheet)workbooksExcel.ActiveSheet;
sheetExcel.Cells[i + 2, j + 1].Value = data;
}
catch (Exception ex) { }
}
}

DevExpress export GridView to Excel

I really need help with this.. I can't find any example on the internet
I am using DevExpress GridView I need to send it to excel and I'm getting problems to loop to every cell and column because DevExpress contains different methods then the DataGridView
that's the code that i'm trying to write.. I really Appreciate your help
public class Form1 : System.Windows.Forms.Form
{
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
string FirstName = "First Name";
string FatherName = "Father Name";
string LastName = "Last Name";
}
public Form1()
{
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 20;
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
private void simpleButton1_Click(object sender, System.EventArgs e)
{
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=C:\\Users\\pc\\Documents\\Emp.xlsx;Extended Properties=\"Excel 12.0;HDR=Yes\"";
con.Open();
DataTable dtSchema;
dtSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
OleDbCommand Command = new OleDbCommand ("select * FROM [" + dtSchema.Rows[0]["TABLE_NAME"].ToString() + "]", con);
OleDbDataAdapter da = new OleDbDataAdapter(Command);
DataSet ds = new DataSet ();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
private void ExportBtn_Click(object sender, System.EventArgs e)
{
for (int i = 1; i < gridView3.Columns.Count + 1; i++)
{
//ExcelApp.Cells[1, i] = gridView3.Columns[i].HeaderStyleName;
}
for (int i = 0; i< gridView3.RowCount - 1; i++)
{
for (int j = 0; j < gridView3.Columns.Count; j++)
{
ExcelApp.Cells[i + 2, j + 1] = gridView3.Columns[j].ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\pc\\Emp.xlsx");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
}
My problem is in the Export button the click event.. There's no such thing as Row()
To know various export methods of XtraGrid, go through Export Methods and Settings
Use GridControl.ExportToXls(String) Method
Example code snippet:
private void mnuExportTable_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
using (SaveFileDialog saveDialog = new SaveFileDialog())
{
saveDialog.Filter = "Excel (2003)(.xls)|*.xls|Excel (2010) (.xlsx)|*.xlsx |RichText File (.rtf)|*.rtf |Pdf File (.pdf)|*.pdf |Html File (.html)|*.html";
if (saveDialog.ShowDialog() != DialogResult.Cancel)
{
string exportFilePath = saveDialog.FileName;
string fileExtenstion = new FileInfo(exportFilePath).Extension;
switch (fileExtenstion)
{
case ".xls":
gridControl.ExportToXls(exportFilePath);
break;
case ".xlsx":
gridControl.ExportToXlsx(exportFilePath);
break;
case ".rtf":
gridControl.ExportToRtf(exportFilePath);
break;
case ".pdf":
gridControl.ExportToPdf(exportFilePath);
break;
case ".html":
gridControl.ExportToHtml(exportFilePath);
break;
case ".mht":
gridControl.ExportToMht(exportFilePath);
break;
default:
break;
}
if (File.Exists(exportFilePath))
{
try
{
//Try to open the file and let windows decide how to open it.
System.Diagnostics.Process.Start(exportFilePath);
}
catch
{
String msg = "The file could not be opened." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath;
MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
String msg = "The file could not be saved." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath;
MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
Reference:
Exporting Several XtraGrid Controls to a Single Excel File
try below code. SaveFileDialog1 is tool
also add required references --
Try
Dim sv As New SaveFileDialog1
SaveFileDialog1.Filter = "Excel Workbook|*.xlsx"
If SaveFileDialog1.ShowDialog() = DialogResult.OK And SaveFileDialog1.FileName <> Nothing Then
If SaveFileDialog1.FileName.EndsWith(".xlsx") Then
Dim path = SaveFileDialog1.FileName.ToString()
GridControlAFP.ExportToXlsx(path)
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlWorkBook = xlApp.Workbooks.Open(path)
xlWorkSheet = xlWorkBook.Sheets("Sheet")
xlWorkSheet.Range("A1:XFD1").EntireColumn.AutoFit()
xlWorkBook.Save()
xlWorkBook.Close()
xlApp.Quit()
End If
MessageBox.Show("Data Exported to :" + vbCrLf + SaveFileDialog1.FileName, "Business Intelligence Portal", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
SaveFileDialog1.FileName = Nothing
End If
Catch ex As Exception
End Try
The selectgridvalues function returns the values in the Database in Datatable format.
Calling this function in the show button sets these values in the gridcontrol.
Export button exports the values into an Excel sheet in .xlsx format
//function to get values from DataTable to gridControl Devexpress
public DataTable selectgridvalues()
{
SqlConnection con;
con = new SqlConnection();
con.ConnectionString = "server='SERVER';uid='sa';pwd='1234';database='DBName'";
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from Tablename order by id ", con);
DataTable dt = new DataTable();
adp.Fill(dt);
gridControl1.DataSource =dt;
}
//code on showdatagridview values button
public void buttonShow_Click(object sender, EventArgs e)
{
gridControl1.DataSource = selectgridvalues();
}
//code on export to excel button
private void buttonExportExcel_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialogExcel = new SaveFileDialog();
saveFileDialogExcel.Filter = "Excel files (*.xlsx)|*.xlsx";
if (saveFileDialogExcel.ShowDialog() == DialogResult.OK)
{
string exportFilePath = saveFileDialogExcel.FileName;
gridControl1.DataSource = selectgridvalues();
gridControl1.ExportToXlsx(exportFilePath);
}
}

Import Excel file into Microsoft SQL Server using C#

I have a C# program that I want to import an Excel file into Microsoft SQL Server 2008 R2.
Can someone give me a link or tutorial where I can fully understand on how to this?
I've been doing this for a long time and I really don't have an idea on how to implement this.
Please help. Thanks.
string excelConnectionString = #"Provider=Microsoft
.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended
Properties=""Excel 8.0;HDR=YES;""";
// Create Connection to Excel Workbook
We can Import excel to sql server like this
using (OleDbConnection connection =
new OleDbConnection(excelConnectionString)){
OleDbCommand command = new OleDbCommand
("Select ID,Data FROM [Data$]", connection);
connection.Open();
`// Create DbDataReader to Data Worksheet `
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=.;
Initial Catalog=Test;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelData";
bulkCopy.WriteToServer(dr);
}
This code may also help you.
private void processExcel(string filename)
{
filename = Server.MapPath("~/Files/WM-0b23-productsBook.xlsx");
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
var missing = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(filename, false, true, missing, missing, missing, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range xlRange = xlWorkSheet.UsedRange;
Array myValues = (Array)xlRange.Cells.Value2;
int vertical = myValues.GetLength(0);
int horizontal = myValues.GetLength(1);
System.Data.DataTable dt = new System.Data.DataTable();
// must start with index = 1
// get header information
for (int i = 1; i <= horizontal; i++)
{
dt.Columns.Add(new DataColumn(myValues.GetValue(1, i).ToString()));
}
// Get the row information
for (int a = 2; a <= vertical; a++)
{
object[] poop = new object[horizontal];
for (int b = 1; b <= horizontal; b++)
{
poop[b - 1] = myValues.GetValue(a, b);
}
DataRow row = dt.NewRow();
row.ItemArray = poop;
dt.Rows.Add(row);
}
// assign table to default data grid view
GridView1.DataSource = dt;
GridView1.DataBind();
xlWorkBook.Close(true, missing, missing);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Check out this post :
Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function
Import Data from Excel to SQL Server
I think that most useful answers already have been given.
I am not going to describe the C# way, but rathher give you an easy way of doing it without C#:
Open your MS Access, link your MS SQL database via ODBC, open your table in Access and paste your Excel records there via copy-paste (preformatted to match the table schema.)
This way is very fast and useful when you do some one-time data import.

Categories

Resources