I have a small button click code which outputs a SQL query to Excel.
This worked fine until I added a new field in the table it queries and now it is still outputting the same fields and not including the new one.
Code:
private void button4_Click(object sender, EventArgs e)
{
string SQLQuery = "SELECT * FROM Services";
SqlConnection conn = new SqlConnection("CONNECTION STRING");
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(SQLQuery, conn);
da.Fill(ds);
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook ExcelWorkBook = null;
Worksheet ExcelWorkSheet = null;
ExcelApp.Visible = true;
ExcelApp.WindowState = XlWindowState.xlMinimized;
ExcelApp.WindowState = XlWindowState.xlMaximized;
ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
List<string> SheetNames = new List<string>();
SheetNames.Add("Services Details");
ExcelApp.ActiveWindow.Activate();
try
{
for (int i = 1; i < ds.Tables.Count; i++)
ExcelWorkBook.Worksheets.Add();
for (int i = 0; i < ds.Tables.Count; i++)
{
int r = 1;
ExcelWorkSheet = ExcelWorkBook.Worksheets[i + 1];
ExcelWorkSheet.Name = "Services";
for (int col = 1; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Columns[col - 1].ColumnName;
r++;
for (int row = 0; row < ds.Tables[i].Rows.Count; row++)
{
for (int col = 1; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Rows[row][col - 1].ToString();
r++;
}
ExcelWorkSheet.Rows[1].EntireRow.Font.Bold = true;
ExcelApp.Columns.AutoFit();
}
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelWorkSheet);
Marshal.ReleaseComObject(ExcelWorkBook);
Marshal.ReleaseComObject(ExcelApp);
}
catch (Exception exHandle)
{
Console.WriteLine("Exception: " + exHandle.Message);
Console.ReadLine();
}
}
I have also tried explicitly querying the new field SELECT ABC FROM Services
and nothing is output.
There are values in the new fields.
If I run the same query on Azure query editor preview I get the correct results.
EDIT
Ok so I changed the query to SELECT *,1 FROM Services
and then I get all the fields (bar the new "1" field) how can I change the loop to get all fields?
EDIT 2 SOLUTION Using EPPlus
Just to update anyone looking at this in the future, I used the NuGet Package Manager
(Project
> Manage NuGet Packages
) and installed EPPlus
by Jan Kallmän.
I then added:
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Data.SqlClient;
using System.IO;
And used the following code on the button:
private void button4_Click(object sender, EventArgs e)
{
string SQLQuery = "SELECT * FROM Services";
SqlConnection conn = new SqlConnection("CONNECTION STRING");
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(SQLQuery, conn);
DataTable dt = new DataTable();
da.Fill(dt);
using (var p = new ExcelPackage())
{
var ws = p.Workbook.Worksheets.Add("Service");
ws.Cells["A1"].LoadFromDataTable(dt, true);
int totalRows = ws.Dimension.End.Row;
int totalCols = ws.Dimension.End.Column;
var headerCells = ws.Cells[1, 1, 1, totalCols];
var headerFont = headerCells.Style.Font;
headerFont.Bold = true;
var allCells = ws.Cells[1, 1, totalRows, totalCols];
allCells.AutoFitColumns();
p.SaveAs(new FileInfo(@"d:\excel\Service" + DateTime.Now.ToFileTime() + ".xlsx"));
}
}
This is an instant output to a file and thanks to @Caius Jard