当前位置:网站首页>ASP. Net daily development notes ---- export to excel
ASP. Net daily development notes ---- export to excel
2022-04-22 07:16:00 【Caramel macchiato】
using Aspose.Cells;
public void btnExcel_Click(object sender, EventArgs e)
{
Workbook workbook = new Workbook();
workbook.Worksheets.Clear();
workbook.Worksheets.Add("sheet1");
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
MergeCell(cells, " Content ", int, int, int, int, workbook);// If you need to merge cells, add this sentence cells.Merge( Start with the line , From which column , Merge several lines , Merge several columns );
-------------------- Loop of merged cells -------------------------
for (int i = 0; i < There are several columns ; i++)
{
for (int j = 0; j <= A total of rows have been merged ; j++)
{
cells[j, i].SetStyle(CreateStyle(workbook));
cells.Columns[i].Width = 15;// Set the width of each cell
}
}
--------------------------------------------------------------------
------------------- Loop without merging ---------------------------------
for (int i = 0; i < 23; i++)
{
cells[0, i].SetStyle(CreateStyle(workbook));
cells.Columns[i].Width = 15;// Set the width of each cell
}
----------------------------------------------------------------------
int rowIndex = The index of the row where the data starts ;
IList<> Data source collection = ;
foreach (var item in Data source collection )
{
MergeCell(cells, item. data 1, rowIndex, 0, 1, 1, workbook);
MergeCell(cells, item. data 2, rowIndex, 1, 1, 1, workbook);
rowIndex++;
}
string exportFileName = HttpUtility.UrlEncode(" Generate excel Name " + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx");
workbook.Save(exportFileName, FileFormatType.Xlsx, SaveType.OpenInExcel, Response);
Response.End();
}
-------- Copy available
/// <summary>
/// merge cell
/// </summary>
/// <param name="cells"> Cell </param>
/// <param name="value"> Displayed value </param>
/// <param name="rBegin"> The first few lines begin </param>
/// <param name="cBegin"> Which column begins </param>
/// <param name="rCount"> How many rows are merged </param>
/// <param name="cCount"> How many columns are merged </param>
public void MergeCell(Cells cells, string value, int rBegin, int cBegin, int rCount, int cCount, Workbook workbook)
{
cells.Merge(rBegin, cBegin, rCount, cCount);
cells[rBegin, cBegin].PutValue(value);
cells[rBegin, cBegin].SetStyle(CreateStyle(workbook));
}
/// <summary>
/// Set the style
/// </summary>
/// <param name="workbook"></param>
/// <returns></returns>
private Aspose.Cells.Style CreateStyle(Workbook workbook)
{
Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.IsTextWrapped = true;
style.Font.IsBold = true;
style.Font.Size = 11;
style.Font.Color = System.Drawing.Color.Black;
style.Pattern = BackgroundType.Solid;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
return style;
}
版权声明
本文为[Caramel macchiato]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220608525754.html
边栏推荐
- Format control of format() method
- Distributed task scheduling and computing framework: powerjob advanced features - container 03
- Matlab: female voice to male voice
- XPath of crawler notes
- Is it difficult to get started with self-study of Digital IC design? How to get started quickly?
- Shift left and right
- Zhejiang University Edition "C language programming (3rd Edition)" topic set exercise 7-4 find out the elements that are not common to two arrays
- Alibaba cloud's deployment of rsshub stepping on the pit notes
- Nacos persistent switch configuration
- Goodbye, postman. One thing to say: apifox is yyds
猜你喜欢
随机推荐
leetcode598:范围求和II
Use of Tencent cloud object storage service
浙大版《C语言程序设计(第3版)》题目集 练习7-4 找出不是两个数组共有的元素
ASP.NET日常开发随手记------后台执行js脚本
[jeecg] modify VISER chart color style
JS realizes clicking avatar to upload picture modification
自己寻找一个记事本文件,数据素材自找,统计整个文本中,某三个关键字或者语句词条出现的次数。
Methods and skills of fast reading papers
安装和修改uTools及vscode插件安装路径
Introduction to IC Analog Layout - learning notes on layout Basics (I)
Nacos service provider registration
Difference between analog IC design and digital IC design, including salary table
1、编写学生信息管理系统以下三个模块:并检测执行。 1、添加学生信息 4、查询学生信息 5、查询全部学生信息
jeecg项目部署笔记
Quotient principle of modular division
Parseexception unparseable date time conversion exception
common-net hp unix系统 ftp服务器 listFiles 返回为空解决方案。
Scope and lifetime (Mr. Weng Kai)
Latex symbol and formula set
How to become an IC Verification Engineer?









