New Post: Closing a ClosedXML generated Workbook.
New Post: Copying range become very slow than copying with Microsoft Interop Service
The copy itself is expensive so there's little to do about it at the moment.
New Post: Adding images to worksheet
Thanks for the help.
New Post: Adding images to worksheet
New Post: Closing a ClosedXML generated Workbook.
Apologies for not having replied sooner, but I couldn't get ahold of the code to test it until today.
As roberttanenbaum said, it was the email sending process not releasing the file. I commented the whole block where the mail was being sent and the File.Delete line worked like a charm.
Thanks for both of your replies, and especially to the ClosedXML crew for making such a wonderful piece of code.
Good day!
New Post: Using foreach to deal with Excel rows and skip the 1st row as header
Since Rows() doesn't work, I used Rows((Func<IXLTableRow, bool>)null) and works fine.
The code changed to
foreach (var row in table.DataRange.Rows((Func<IXLTableRow, bool>)null))
{
var name = row.Field("Name").GetString();
}
Thank you all!New Post: Adding images to worksheet
New Post: Does ClosedXML instantiate a new instance of Excel?
New Post: Does ClosedXML instantiate a new instance of Excel?
To create the file you need to do a workbook.SaveAs() call to save the workbook to a file.
Put a try/catch around the SaveAs to see if it is getting any error and log the error.
New Post: Does ClosedXML instantiate a new instance of Excel?
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
New Post: Validation of cell using fixed List of values
https://closedxml.codeplex.com/wikipage?title=Data%20Validation
ws.Cell("C1").Value = "Yes";
ws.Cell("C2").Value = "No";
ws.Cell("A5").DataValidation.List(ws.Range("C1:C2"));
New Post: Does ClosedXML instantiate a new instance of Excel?
Put a log before and after workbook.SaveAs
New Post: Validation of cell using fixed List of values
New Post: Does ClosedXML instantiate a new instance of Excel?
.
.
.
.
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);
}
New Post: Does ClosedXML instantiate a new instance of Excel?
New Post: Does ClosedXML instantiate a new instance of Excel?
Created Unassigned: Datetime text has been cuted! [9315]
and if the text is "6:00" it will automatically recognized as time format and set to "6:00:00".
This makes me very annoying ! How can i set the content as original as it is?
New Post: How to read two tables from Excel with C#?
I was reading a documentation in here, but I didn't really get it, how to actually read two tables in Excel with C# using ClosedXML? My spreadhseet lokks like this:

I need to read those TWO tables in C#, and when, basically, convert them to XML file.
So the question is how to read those two tables from Excel using C# ClosedXML library? Thank you.
New Post: How to read two tables from Excel with C#?
worksheet.Table(int32TableIndex)
worksheet.Tables = collection of tables in the worksheet
Closed Unassigned: Datetime text has been cuted! [9315]
and if the text is "6:00" it will automatically recognized as time format and set to "6:00:00".
This makes me very annoying ! How can i set the content as original as it is?
Comments: This is the bane of my existence. In the beginning I tried to make ClosedXML behave as close to Excel as possible. That wasn't such a good idea after all. In your case the text is being converted to DateTime values (like Excel does). You have 3 options: #1 Format the date in the cells #2 Use cell.SetValue(string) instead #3 put an apostrophe at the beginning of the string e.g. '6:00'