I already did that and there is no error. It works OK on my laptop (but then again, I have Excel – thus the reason why I thought it was req’d). I attached the snippet of code that does this. I have plenty of logging in it to trap any errors. None found.
var workbook =newXLWorkbook();
wsname = wsname + PayDate.ToString("MMDDYYYY");
fn = sFilePath + sNomenclature +".xlsx";
var worksheet = workbook.Worksheets.Add(wsname);
sql ="SELECT EmployeeID, PayDate, CostCenter4, CostCenter5, Costcenter6, Qty, PieceWork "+
"FROM WRFXUpld "+
"WHERE cast(PayDate as smalldatetime) >= cast(@PayDate as smalldatetime) "+
"ORDER BY EmployeeID, PayDate, CostCenter6, CostCenter5 ";
SqlConnection mySQLConnection =newSqlConnection(sConnectionString);
mySQLConnection.Open();
SqlCommand mySQLCommand =newSqlCommand(sql, mySQLConnection);
mySQLCommand.Parameters.Add("@PayDate",SqlDbType.Char);
mySQLCommand.Parameters["@PayDate"].Value = cPayDate;
try
{
string s15 ="Attempting to SELECT fields from WRFXUpld for the spreadsheet...";
Log(s15);
SqlDataReader mySQLReader = mySQLCommand.ExecuteReader();
string s16 ="Finished SELECT fields for the spreadsheet...";
Log(s16);
if (mySQLReader.Read())
{
string indcell ="";
int i = 1;
string s17 ="Going into loop to populate spreadsheet...";
Log(s17);
while (mySQLReader.Read())
{
indcell ="A"+ i.ToString();
worksheet.Cell(indcell).Value = mySQLReader["EmployeeID"].ToString().ToUpper().Trim();
string s17a ="In the spreadsheet loop for EmployeeID = "+ mySQLReader["EmployeeID"].ToString().ToUpper().Trim();
Log(s17a);
indcell ="B"+ i.ToString();
worksheet.Cell(indcell).Value = mySQLReader["PayDate"].ToString().ToUpper().Trim();
indcell ="C"+ i.ToString();
worksheet.Cell(indcell).Value = mySQLReader["CostCenter4"].ToString().ToUpper().Trim();
indcell ="D"+ i.ToString();
worksheet.Cell(indcell).Value = mySQLReader["CostCenter5"].ToString().ToUpper().Trim();
indcell ="E"+ i.ToString();
worksheet.Cell(indcell).Value = mySQLReader["CostCenter6"].ToString().ToUpper().Trim();
indcell ="F"+ i.ToString();
worksheet.Cell(indcell).Value = mySQLReader["Qty"];
indcell ="G"+ i.ToString();
worksheet.Cell(indcell).Value = mySQLReader["PieceWork"];
i = i + 1;
}
string s18 ="Finished looping to populate spreadsheet...";
Log(s18);
mySQLReader.Close();
mySQLReader.Dispose();
workbook.SaveAs(fn);
string s19 ="SAVED the spreadsheet!!!";
Log(s19);
}
else
{
string s20 ="No spreadsheet to save due to no data.";
Log(s20);
}
mySQLConnection.Close();
mySQLConnection.Dispose();
}
catch (Exception e4)
{
mySQLConnection.Close();
mySQLConnection.Dispose();
string s ="Unable to create spreadsheet due to: "+ e4.Message;
Log(s);
}
}
Thanks!
Mark Skinnell
ITP Sr. Project Manager
Office: 828-322-6261
www.itpbarcode.com