我正在尝试重新排列我在C#中创建的Excel工作簿中的工作表顺序;我希望产品工作表在工作簿中显示为1st。我试过“移动”,但它没有得到认可。非常感谢任何想法!
public static bool ExportReportToExcel(DataSet ds, string fileName,
DateTime startDate, DateTime endDate)
{
bool isSuccessful = false;
try
{
Console.WriteLine("Creating Excel File at {0}", DateTime.Now.ToString());
// Save file after processing to local folder (in case
// network location is blocked, we will still have the file)
if (File.Exists(fileName)) { File.Delete(fileName); }
// create new workbook for the new source
XLWorkbook wb = new XLWorkbook();
// Set default font for workbook
wb.Style.Font.FontName = "Arial";
wb.Style.Font.FontSize = 8;
List<Definition> definitions = GetDefinitions(ds.Tables[1]);
IXLWorksheet ws = wb.AddWorksheet("Products");
WriteHeaders(ws, ds, startDate, endDate, definitions);
WriteTotals(ws, startDate, endDate);
WriteDetails(ws, ds, startDate, endDate);
IXLWorksheet defWS = Tools.AddDefinitionsWorksheet(wb, definitions,
"Product Report", 4, 60, false);
WriteSourcesToDefinitions(defWS, ds);
// save file
wb.SaveAs(fileName);
isSuccessful = true;
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message + "\n" + ex.StackTrace);
string logFileName = ConfigurationManager.AppSettings["ErrorLogFile"];
Tools.LogToFile(logFileName, "ProductRpt", "Exception: " + ex.Message +
"\n" + ex.StackTrace);
}
return isSuccessful;
}
答案 0 :(得分:1)
您需要使用Position属性。尝试添加
ws.Position = 1;
这里是参考:https://closedxml.codeplex.com/wikipage?title=Organizing%20Sheets&referringTitle=Documentation
答案 1 :(得分:0)
在XLWorkbook
class:
public IXLWorksheet AddWorksheet(String sheetName, Int32 position)
{
return Worksheets.Add(sheetName, position);
}
如您所见,您可以将所需位置传递给此功能