我正在使用EPPlus在我的MVC项目中创建一个Excel文件。我想创建一个包含30列的电子表格。
使用我当前的代码,电子表格最多可以显示26列(名为A - Z)。我知道我需要更改范围以包括AA - ZZ列,但我不知道该怎么做。我应该用int来引用列吗?我在GitHub网站上看不到任何这样的样本。
这是我的代码(来自数据表(dt)的Excel数据):
//return dt;
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Claims");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(dt, true);
//prepare the range for the column headers
string cellRange = "A1:" + Convert.ToChar('A' + dt.Columns.Count - 1) + 1;
//Format the header for columns
using (ExcelRange rng = ws.Cells[cellRange])
{
rng.Style.WrapText = false;
rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
rng.Style.Font.Bold = true;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
//Set Pattern for the background to Solid
rng.Style.Fill.BackgroundColor.SetColor(Color.Yellow);
rng.Style.Font.Color.SetColor(Color.Black);
}
//prepare the range for the rows
string rowsCellRange = "A2:" + Convert.ToChar('A' + dt.Columns.Count - 1) + dt.Rows.Count * dt.Columns.Count;
//Format the rows
using (ExcelRange rng = ws.Cells[rowsCellRange])
{
rng.Style.WrapText = true;
rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
}
//Read the Excel file in a byte array
Byte[] fileBytes = pck.GetAsByteArray();
//Clear the response
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Cookies.Clear();
//Add the header & other information
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.CacheControl = "private";
Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
Response.AddHeader("content-disposition", "attachment;filename=Claims.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Write it back to the client
Response.BinaryWrite(fileBytes);
Response.End();
}
答案 0 :(得分:0)
首先,正如我在您的评论中所看到的那样,workSmarter表示您可以使用ws.Cells[row,col]
格式来处理单元格。
但您也可以使用EPPlus在Excel中加载通用IEnumerable(IEnumerable)。这是一种使用LoadFromCollection
方法在Excel中编写通用IEnumerable的方法。在这个例子中,我使用反射来创建基于T属性的标题行:
public FileContentResult WriteToExcel<T>(IEnumerable<T> data, string fileName, bool shouldGenerateSumRow = true)
{
using (var package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("گزارش");
ws.View.RightToLeft = true;
var props = TypeDescriptor.GetProperties(typeof(T));
var RowCount = data.Count();
ws.Cells.LoadFromCollection(data, true, OfficeOpenXml.Table.TableStyles.Medium27);
ws.InsertRow(1, 1);
var excelRange = ws.Cells[1, 1, 1, props.Count];
ws.Row(1).Height = 80;
excelRange.Merge = true;
excelRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.CenterContinuous;
excelRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
excelRange.Style.WrapText = true;
excelRange.Value = fileName;
for (var i = 0; i < props.Count; i++)
{
ws.Cells[2, i + 1].Value = props[i].DisplayName;
}
ws.Cells.AutoFitColumns();
var fcr = new FileContentResult(package.GetAsByteArray(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = $"{fileName}.xlsx"
};
return fcr;
}
}
答案 1 :(得分:0)
使用整数以便于使用。 格式:“ws.Cells [row,column]”
使用此代码设置标题:
using (var range = ws.Cells[1, 1, 1, 11]) //format: ws.Cells[int FromRow, int FromCol, int ToRow, int ToCol]
{
range.Style.Font.Bold = true;
range.Style.ShrinkToFit = false;
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
range.AutoFilter = true;
}