Hello All,
I am trying to export to excel using insert command. All operations are working fine but when I export to excel, the excel file cell style is changed automatically to Bold for example. If I use column by column insertion, then no issue for cell style.
I want my default cell style while export operation. How can I solve this issue.
Please help to solve this issue.
For more information, I am providing my code and exported excel file image
.
private void button5_Click_1(object sender, RoutedEventArgs e)
{
string templatePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Acty System\\Templates\\Employess1.xls");
string directoryPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Acty System\\Test\\test.xls");
string excelConnectionString = string.Format(CultureInfo.InvariantCulture, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"HDR=YES;\";Jet OLEDB:Engine Type=37", directoryPath);
OleDbCommand command = new OleDbCommand();
OleDbConnection connection = new OleDbConnection(excelConnectionString);
try
{
//Copy from excel template and create a new excel file in selected location
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Interactive = false;
Microsoft.Office.Interop.Excel.Workbook workBook = null;
excel.Visible = false;
workBook = excel.Workbooks.Open(templatePath, Type.Missing);
workBook.SaveCopyAs(directoryPath); //// 97-2003 xls format
//Fetch records
var query = this.dc.Employees.Select(p => new { p.FirstName, p.LastName, p.EmployeeID }).AsEnumerable();
connection.Open();
string commandString = string.Empty;
command = new OleDbCommand(commandString, connection);
foreach (var item in dc.Employees)
{
commandString = "Insert into [PurchaseInvHD$] (FirstName,LastName,EmployeeID) values('" + item.FirstName + "','" + item.LastName + "','" + item.EmployeeID + "')";
command.CommandText = commandString;
command.ExecuteNonQuery();
}
connection.Close();
MessageBox.Show("Expprted");
}
catch
{
connection.Close();
MessageBox.Show("Error");
}
}Regards, Ranjith T Rajan Acty System India Pvt Ltd.