Commit 2197f5ed by ruyun.zhang

下载样式调整

parent 3a055eff
...@@ -425,23 +425,23 @@ private string ValueFormating(decimal? value, string format) ...@@ -425,23 +425,23 @@ private string ValueFormating(decimal? value, string format)
{ {
if (format == UniteDept.Format.整数百分比.ToString()) if (format == UniteDept.Format.整数百分比.ToString())
{ {
return string.Format("{0:P0}", total); return string.Format("{0:0%}", total);
} }
else if (format == UniteDept.Format.一位小数百分比.ToString()) else if (format == UniteDept.Format.一位小数百分比.ToString())
{ {
return string.Format("{0:P1}", total); return string.Format("{0:0.#%}", total);
} }
else if (format == UniteDept.Format.两位小数百分比.ToString()) else if (format == UniteDept.Format.两位小数百分比.ToString())
{ {
return string.Format("{0:P2}", total); return string.Format("{0:0.##%}", total);
} }
else if (format == UniteDept.Format.三位小数百分比.ToString()) else if (format == UniteDept.Format.三位小数百分比.ToString())
{ {
return string.Format("{0:P3}", total); return string.Format("{0:0.###%}", total);
} }
else if (format == UniteDept.Format.四位小数百分比.ToString()) else if (format == UniteDept.Format.四位小数百分比.ToString())
{ {
return string.Format("{0:P4}", total); return string.Format("{0:0.####%}", total);
} }
else if (Math.Abs(total) < 1.5m) else if (Math.Abs(total) < 1.5m)
{ {
...@@ -618,13 +618,14 @@ public string ExcelDownload(UniteDeptDetailResponse uniteDeptDetail, string name ...@@ -618,13 +618,14 @@ public string ExcelDownload(UniteDeptDetailResponse uniteDeptDetail, string name
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate)) using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate))
using (ExcelPackage package = new ExcelPackage(fs)) using (ExcelPackage package = new ExcelPackage(fs))
{ {
var worksheet = package.Workbook.Worksheets.Add(name); var worksheet = package.Workbook.Worksheets.Add(uniteDeptDetail.Title);
worksheet.View.ShowGridLines = false;
var maxColIndex = GetMaxColumnIndex(0, uniteDeptDetail.DetailItems); var maxColIndex = GetMaxColumnIndex(0, uniteDeptDetail.DetailItems);
worksheet.SetValue(1, 1, uniteDeptDetail.Title); worksheet.SetValue(1, 1, uniteDeptDetail.Title);
worksheet.Cells[1, 1, 1, maxColIndex].Merge = true; worksheet.Cells[1, 1, 1, maxColIndex].Merge = true;
worksheet.Cells[1, 1, 1, maxColIndex].Style.Font.Bold = true; worksheet.Cells[1, 1, 1, maxColIndex].Style.Font.Bold = true;
worksheet.Cells[1, 1, 1, maxColIndex].Style.Font.Size = 20; worksheet.Cells[1, 1, 1, maxColIndex].Style.Font.Size = 18;
worksheet.Cells[1, 1, 1, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin); worksheet.Cells[1, 1, 1, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin);
worksheet.Cells[1, 1, 1, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center; worksheet.Cells[1, 1, 1, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
worksheet.Cells[1, 1, 1, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells[1, 1, 1, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
...@@ -646,9 +647,13 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis ...@@ -646,9 +647,13 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis
{ {
++rowIndex; ++rowIndex;
worksheet.SetValue(rowIndex, 1, $"{item.Title} : {item.TotalFormat}"); worksheet.SetValue(rowIndex, 1, $"{item.Title} : {item.TotalFormat}");
if (!string.IsNullOrEmpty(item.Remark))
{
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].AddComment(item.Remark, "System");
}
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Merge = true; worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Merge = true;
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Font.Bold = true; worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Font.Bold = true;
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Font.Size = 18; worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Font.Size = 14;
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin); worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin);
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center; worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left; worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
...@@ -667,7 +672,7 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis ...@@ -667,7 +672,7 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis
worksheet.SetValue(rowIndex, colIndex + 1, headers.ElementAt(colIndex)); worksheet.SetValue(rowIndex, colIndex + 1, headers.ElementAt(colIndex));
worksheet.Cells[rowIndex, colIndex + 1].Style.Font.Bold = true; worksheet.Cells[rowIndex, colIndex + 1].Style.Font.Bold = true;
worksheet.Cells[rowIndex, colIndex + 1].Style.Font.Size = 16; worksheet.Cells[rowIndex, colIndex + 1].Style.Font.Size = 11;
worksheet.Cells[rowIndex, colIndex + 1].Style.Border.BorderAround(ExcelBorderStyle.Thin); worksheet.Cells[rowIndex, colIndex + 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
worksheet.Cells[rowIndex, colIndex + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
worksheet.Cells[rowIndex, colIndex + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
...@@ -678,7 +683,7 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis ...@@ -678,7 +683,7 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Merge = true; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Merge = true;
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Font.Bold = true; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Font.Bold = true;
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Font.Size = 16; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Font.Size = 11;
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin); worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin);
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
...@@ -691,32 +696,30 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis ...@@ -691,32 +696,30 @@ int WriteSheetCells(ExcelWorksheet worksheet, int maxColIndex, int rowIndex, Lis
rowIndex = rowIndex + 1; rowIndex = rowIndex + 1;
for (int colIndex = 0; colIndex < headers.Count(); colIndex++) for (int colIndex = 0; colIndex < headers.Count(); colIndex++)
{ {
object cellValue = row[headers.ElementAt(colIndex)]; // 获取单元格的值 var cellValue = row[headers.ElementAt(colIndex)]?.ToString() ?? ""; // 获取单元格的值
double numericValue; // 用于存储转换后的数值 double numericValue = 0;
if (cellValue.EndsWith("%") && double.TryParse(cellValue.Replace("%", ""), out numericValue))
if (cellValue != null && double.TryParse(cellValue.ToString(), out numericValue)) {
worksheet.SetValue(rowIndex, colIndex + 1, numericValue / 100);
worksheet.Cells[rowIndex, colIndex + 1].Style.Numberformat.Format = "0.00%";
}
else if (double.TryParse(cellValue, out numericValue))
{ {
if (numericValue < 0)
{
worksheet.Cells[rowIndex, colIndex + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[rowIndex, colIndex + 1].Style.Fill.BackgroundColor.SetColor(Color.Red);
}
worksheet.SetValue(rowIndex, colIndex + 1, numericValue); worksheet.SetValue(rowIndex, colIndex + 1, numericValue);
worksheet.Cells[rowIndex, colIndex + 1].Style.Numberformat.Format = "0.00";
} }
else else
{ {
worksheet.SetValue(rowIndex, colIndex + 1, row[headers.ElementAt(colIndex)]); worksheet.SetValue(rowIndex, colIndex + 1, cellValue);
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[rowIndex, 1, rowIndex, maxColIndex].Style.Fill.BackgroundColor.SetColor(Color.GhostWhite);
} }
worksheet.Cells[rowIndex, colIndex + 1].Style.Font.Size = 16; worksheet.Cells[rowIndex, colIndex + 1].Style.Font.Size = 11;
worksheet.Cells[rowIndex, colIndex + 1].Style.Border.BorderAround(ExcelBorderStyle.Thin); worksheet.Cells[rowIndex, colIndex + 1].Style.Border.BorderAround(ExcelBorderStyle.Thin);
worksheet.Cells[rowIndex, colIndex + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
worksheet.Cells[rowIndex, colIndex + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
if (colIndex + 1 == headers.Count()) if (colIndex + 1 == headers.Count())
{ {
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Merge = true; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Merge = true;
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Font.Size = 16; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Font.Size = 11;
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin); worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.Border.BorderAround(ExcelBorderStyle.Thin);
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; worksheet.Cells[rowIndex, colIndex + 1, rowIndex, maxColIndex].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment