C# Aspose.Cells.dll Excel操作总结

时间:2023-03-09 20:27:12
C# Aspose.Cells.dll Excel操作总结

简介

Aspose.Cells是一款功能强大的 Excel 文档处理和转换控件,不依赖 Microsoft Excel 环境,支持所有 Excel 格式类型的操作。

下载 Aspose.Cells.dll

获取Excel数据

Workbook workbook = new Workbook("E:\\test.xlsx");
Cells cells = workbook.Worksheets[].Cells;
for (int i = ; i < cells.MaxDataRow + ; i++)
{
for (int j = ; j < cells.MaxDataColumn + ; j++)
{
string s = cells[i, j].StringValue.Trim();
//一行行的读取数据,插入数据库的代码也可以在这里写
}
}

返回DataTable数据

Workbook workbook = new Workbook("E:\\test.xlsx");
Cells cells = workbook.Worksheets[].Cells;
System.Data.DataTable dataTable1 = cells.ExportDataTable(, , cells.MaxDataRow, cells.MaxColumn);//没有标题
System.Data.DataTable dataTable2 = cells.ExportDataTable(, , cells.MaxDataRow + , cells.MaxColumn, true);//有标题

无标题

C# Aspose.Cells.dll Excel操作总结

有标题

C# Aspose.Cells.dll Excel操作总结

使用小结

生成Excel

/// <summary>
/// DataTable数据导出Excel
/// </summary>
/// <param name="data"></param>
/// <param name="filepath"></param>
public static void DataTableExport(DataTable data, string filepath)
{
try
{
//Workbook book = new Workbook("E:\\test.xlsx"); //打开工作簿
Workbook book = new Workbook(); //创建工作簿
Worksheet sheet = book.Worksheets[]; //创建工作表
Cells cells = sheet.Cells; //单元格
//创建样式
Aspose.Cells.Style style = book.Styles[book.Styles.Add()];
style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线
style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线
style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线
style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
style.HorizontalAlignment = TextAlignmentType.Center; //单元格内容的水平对齐方式文字居中
style.Font.Name = "宋体"; //字体
style1.Font.IsBold = true; //设置粗体
style.Font.Size = ; //设置字体大小
//style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色
//style.Pattern = Aspose.Cells.BackgroundType.Solid; //背景样式
//style.IsTextWrapped = true; //单元格内容自动换行 //表格填充数据
int Colnum = data.Columns.Count;//表格列数
int Rownum = data.Rows.Count;//表格行数
//生成行 列名行
for (int i = ; i < Colnum; i++)
{
cells[, i].PutValue(data.Columns[i].ColumnName); //添加表头
cells[, i].SetStyle(style); //添加样式
//cells.SetColumnWidth(i, data.Columns[i].ColumnName.Length * 2 + 1.5); //自定义列宽
//cells.SetRowHeight(0, 30); //自定义高
}
//生成数据行
for (int i = ; i < Rownum; i++)
{
for (int k = ; k < Colnum; k++)
{
cells[ + i, k].PutValue(data.Rows[i][k].ToString()); //添加数据
cells[ + i, k].SetStyle(style); //添加样式
}
cells[ + i, ].Formula = "=B" + ( + i) + "+C" + ( + i);//给单元格设置计算公式,计算班级总人数
}
sheet.AutoFitColumns(); //自适应宽
book.Save(filepath); //保存
GC.Collect();
}
catch (Exception e)
{
logger.Error("生成excel出错:" + e.Message);
}
}

调用Excel(例子)

public void DownExcel()
{
//创建DataTable
DataTable dt = new DataTable("Table_AX");
dt.Columns.Add("班级名称", System.Type.GetType("System.String"));
dt.Columns.Add("男生人数", System.Type.GetType("System.String"));
dt.Columns.Add("女生人数", System.Type.GetType("System.String"));
dt.Columns.Add("今日请假", System.Type.GetType("System.String"));
dt.Columns.Add("今日迟到", System.Type.GetType("System.String"));
Random ran = new Random();
for (int i = ; i < ; i++)
{
DataRow dr = dt.NewRow();
dr["班级名称"] = "软件技术" + i + "班";
dr["男生人数"] = ran.Next(, );
dr["女生人数"] = ran.Next(, );
dr["今日请假"] = ran.Next(, );
dr["今日迟到"] = ran.Next(, );
dt.Rows.Add(dr);
}
//地址
string fileName = "班级概况.xls";
string filePath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "GxContent/data/student/" + fileName + "";
//生成Excel
ExcelHelper.DataTableExport(dt, filePath);
//下载
if (System.IO.File.Exists(filePath))
{
try
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
//每次读取文件,只读取1M,这样可以缓解服务器的压力
const long ChunkSize = ;
byte[] buffer = new byte[ChunkSize]; Response.Clear();
//获取文件
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
//获取下载的文件总大小
long dataLengthToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
using (iStream)//解决文件占用问题,using 外 iStream.Dispose() 无法释放文件
{
while (dataLengthToRead > && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, , lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
iStream.Dispose();
iStream.Close();
}
Response.End();
}
}
catch
{
Response.Write("<script>alert('文件未占用或文件未生成,请稍后重试!');window.close();</script>");
}
}
else
{
Response.Write("<script>alert('文件还未生成完,请稍后重试!');window.close();</script>");
}
}

效果

C# Aspose.Cells.dll Excel操作总结


相关文章:C# 读写Excel的一些方法,Aspose.Cells.dll