ExcelHelper----根据指定样式的数据,生成excel(一个sheet1页)文件流

时间:2024-08-17 23:35:02
 /// <summary>
/// Excel导出类
/// </summary>
public class ExcelHelper
{
/// <summary>
/// 根据指定样式的数据,生成excel(一个sheet1页)文件流
/// </summary>
/// <param name="data">待导出数据的数组(包括隐藏的数据)</param>
/// <param name="title">The title.</param>
/// <param name="header">The header.</param>
/// <param name="isShowRowNo">.如果定义为null或者empty,那么表示不需要增加序号列</param>
/// <param name="colVisibleFlagArray">待导出数据的没列的隐藏标识</param>
/// <param name="autosize">自动单元格宽度调整</param>
/// <returns>MemoryStream.</returns>
public static MemoryStream ExportFromArray(string[][] data, string title, string[] header, bool isShowRowNo, bool[] colVisibleFlagArray,bool autosize = true)
{
string[][] newdata = new string[data.Length][];
List<List<string>> newlist = new List<List<string>>();
List<string> newheader = new List<string>();
//虑隐藏的数据:
//过滤隐藏的数据头
for (int i = ; i < colVisibleFlagArray.Length; i++)
{
if (colVisibleFlagArray[i] == true)
{
newheader.Add(header[i]);
}
}
//过滤隐藏列的数据
for (int j = ; j < data.Length; j++)
{
newdata[j] = new string[newheader.Count];
//当前列数
int cnt = ;
//只添加加显示列的数据
for (int i = ; i < colVisibleFlagArray.Length; i++)
{
if (colVisibleFlagArray[i] == true)
{
newdata[j][cnt] = data[j][i];
cnt++;
}
}
}
//如果显示序号,序号列头指定为“序号”;否则不输出列头
string RowNo = isShowRowNo ? "序号" : null;
MemoryStream stream = new MemoryStream();
ExportExcelFromData instance = new ExportExcelFromData();
SheetDefine sheet=new SheetDefine("sheet1", title, RowNo, newheader.ToArray(), newdata);
sheet.AutoSizeColumn = autosize;
instance.AddSheet(sheet); instance.WriteExcel(stream); return stream;
}
}