DoddleReport-创新互联
public class ExcelReportWriter : IReportWriter
网页标题:DoddleReport-创新互联
当前路径:http://azwzsj.com/article/coeepi.html
中自定义Sheet名字
创新互联公司-专业网站定制、快速模板网站建设、高性价比白塔网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式白塔网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖白塔地区。费用合理售后完善,十余年实体公司更值得信赖。report.RenderHints["SheetName"] = "Test Sheet";
var sheetName = report.RenderHints[SheetName] as string ?? "Data";
private void WriteReport(Report report, XLWorkbook workbook)
{
var sheetName = report.RenderHints[SheetName] as string ?? "Data";//需要传入,否则默认
IXLWorksheet worksheet;
int duplicateNameCount = 0;
var originalSheetName = sheetName;
while (workbook.Worksheets.TryGetWorksheet(sheetName, out worksheet))//判断是否有这个sheet
{
sheetName= originalSheetName + ++duplicateNameCount;
}
worksheet= workbook.Worksheets.Add(sheetName);
worksheet.SetShowRowColHeaders(true);
var orientation = report.RenderHints.Orientation == ReportOrientation.Portrait ? XLPageOrientation.Portrait : XLPageOrientation.Landscape;
worksheet.PageSetup.PageOrientation= orientation;
// Set the paper size to what the render hint is set to if (report.RenderHints[PaperSize] != null)
{
worksheet.PageSetup.PaperSize= (XLPaperSize)Enum.Parse(typeof(XLPaperSize), report.RenderHints[PaperSize].ToString());
}
if (report.RenderHints.FreezePanes)
worksheet.SheetView.Freeze(report.RenderHints.FreezeRows, report.RenderHints.FreezeColumns);
// Render the header var fieldsCount = report.DataFields.Count(f => !f.Hidden);
int rowCount = RenderHeader(worksheet, fieldsCount, report.TextFields, report.RenderHints);
// Render all the rows foreach (var row in report.GetRows())
{
rowCount++;
var dataRow = worksheet.Row(rowCount);
RenderRow(rowCount, row, dataRow);
}
// Render the footer RenderFooter(worksheet, fieldsCount, report.TextFields, report.RenderHints, rowCount);
// TODO: AdjustToContents renders horribly when deployed to an Azure Website, need to determine why
// Adjust the width of all the columns for (int i = 0; i < fieldsCount; i++)
{
var reportField = report.DataFields.Where(f => !f.Hidden).Skip(i).Take(1).Single();
var width = new int[] { reportField.DataStyle.Width, reportField.FooterStyle.Width, reportField.HeaderStyle.Width }.Max();
var adjustToContents = report.RenderHints[AdjustColumnWidthToContents] as bool? ?? true;
if (adjustToContents || width > 0)
{
var column = worksheet.Column(i + 1);
if (adjustToContents && width > 0)
{
column.AdjustToContents(width.PixelsToUnits(column.Style.Font),double.MaxValue);
}
else if (adjustToContents)
{
column.AdjustToContents(1, 50, 5.0, 100.0);
}
else
{
column.Width= width.PixelsToUnits(column.Style.Font);
}
}
}
worksheet.Columns().AdjustToContents();
// Check if the current writer needs to append another report to the report we just generated if (ReportsToAppend.ContainsKey(report))
{
foreach (var reportToAppend in ReportsToAppend[report])
{
WriteReport(reportToAppend, workbook);
}
}
}
网页标题:DoddleReport-创新互联
当前路径:http://azwzsj.com/article/coeepi.html