Aspose.cell C# 操作excel(通过批注信息给单元格赋值、批注信息获取公式得出结果并转PNG)

时间:2023-02-01 01:34:51

if (fileName == "") return "";
var CurrentRow = 0;

Workbook work = new Workbook(fileName);
var sheet = work.Worksheets[0];

string name = "";
if (sheet != null)
{
name = sheet.Name;
//获取建设期
var cp = 10;
var rowCount = sheet.Cells.MaxDataRow; //竖着数最后一行的标号
for (var i = sheet.Cells.MinColumn; i <= rowCount; i++)
{
//sql参数化
var Par = new List<SqlParameter>();
#region 获取当前行的启起值和结束值(模块)
var row = sheet.Cells.GetRow(i);//获取当前行的启起值和结束值
if (row.FirstCell == null) continue;
var totalRow = 0; //记录合计当前行
#region 获取一行的启起值和一行的结束值作为循环条件(模块)
//获取一行的启起值和一行的结束值作为循环条件//int cellCount = row.LastCellNum; //一行最后一个cell的编号 即总的列数
for (int j = sheet.Cells.MinDataColumn; j < sheet.Cells.MaxDataColumn; j++)
{
#region 判断当前单元格数据不等空(模块)

if (sheet.Cells[i, j].StringValue.Trim()== null || sheet.Comments[i, j] == null) continue;
var sd = sheet.Cells[i, j].StringValue.Trim();//I当前行J当前列

#region 判断当前单元格批注不等空(模块)

var annotation = ((sheet.Comments[i, j].Note.ToString().Replace("\n", "")).Replace("\r", "")).Trim();//获取当前单元格的批注
if (annotation.IndexOf('=')>-1)
{

sheet.Cells[(sheet.Cells[i, j]).Name].Formula = "=SUM(D7:O7)";
work.CalculateFormula();
string value = sheet.Cells[(sheet.Cells[i, j]).Name].Value.ToString();
//sheet.Cells[(sheet.Cells[i, j]).Name].PutValue(value);
break;
}
#region 获取excel起启行结束行
//如果相等记录当前行
if (annotation.Equals("open", StringComparison.OrdinalIgnoreCase))
{
CurrentRow = i; //记录当前excel行
}
else
{

//解析单元格批注内容
string[] dataArr = annotation.Split('_');
string symbol = "";
string[] special = new string[0];
if (dataArr.Length < 1) continue;
Par.Add(new SqlParameter("@SID", R_ID));
var arr = dataArr[1].Split('|');//获取值判断是否排除建设期
if (arr.Length > 1)
{
//不排除建设期
Par.Add(new SqlParameter("@gid", arr[0]));
}
else
{
var ffda = dataArr[1].Split('~');//判断是否有符号
if (ffda.Length > 1)
{
symbol = ffda[1];
Par.Add(new SqlParameter("@gid", ffda[0]));
}
else
{
//判断是否特殊赋值方式
special = dataArr[1].Split('\\');
Par.Add(special.Length > 1
? new SqlParameter("@gid", special[0])
: new SqlParameter("@gid", dataArr[1]));
}
}
//执行存储过程
var data = CommFunHelper.GetDataTableByKeyValue(DbHelperSQL.RunStoredProcedure(dataArr[0].ToString(), Par.ToArray()));
if (data.Count <= 0) continue;
var nums = 0;
var value = "";
#region 往excel赋值部分

for (var k = 0; k <= (sheet.Cells.GetRow(CurrentRow)).LastDataCell.Column; k++)
{

var va = sheet.Cells.GetRow(CurrentRow)[k].StringValue;
if (va == null) continue;
var num = System.Text.RegularExpressions.Regex.Replace(va.ToString(),
@"[^0-9]+", "");
if (!data.ContainsKey(num)) continue;
nums++;
if (special.Length > 1)
{

if (nums <= Convert.ToInt32(cp.Constant))
{
if (!string.IsNullOrEmpty(data[num]))
{
sheet.Cells[i, j + k].PutValue(Convert.ToDouble(data[num])); //当前行
sheet.Cells[i + 1, j + k]
.PutValue(Convert.ToDouble(data[num])); //当前行的下一行
}
else
{
sheet.Cells[i, j + k].PutValue(data[num]); //当前行
sheet.Cells[i + 1, j + k].PutValue(data[num]); //当前行的下一行
}
}
else
{
if (!string.IsNullOrEmpty(value))
{
sheet.Cells[i, j + k].PutValue(Convert.ToDouble(data[num])); //当前行
sheet.Cells[i + 1, j + k]
.PutValue(Convert.ToDouble(data[num])); //当前行的下一行
}
else
{
sheet.Cells[i, j + k].PutValue(data[num]); //当前行
sheet.Cells[i + 1, j + k].PutValue(data[num]); //当前行的下一行
}
}
}
else
{
value = data[num];
if (arr.Length <= 1)
{
if (nums <= Convert.ToInt32(cp.Constant))
{
value = "";
}
}

if (!string.IsNullOrEmpty(value))
{
sheet.Cells[i, j + k].PutValue(Convert.ToDouble(data[num])); //当前行
}
else
{
sheet.Cells[i, j + k].PutValue(data[num]); //当前行
}

}
}

#endregion
}
#endregion

#endregion

#endregion
}
#endregion

#endregion
}

sheet.ClearComments();//清除所有批注信息
#region 保存文件
var filePath = ConfigurationSettings.AppSettings["ExSave"];
var FileName = R_ID + "_" + az + name + "_" + ".xlsx";
var strFullName = Server.MapPath(filePath) + "\\" + FileName;
work.Save(strFullName);
XlsxToImg(work, Path.ChangeExtension(strFullName, ".png"));
#endregion
return filePath + "/" + Path.ChangeExtension(FileName, ".png");
}

/// <summary>
/// xlsx转图片
/// </summary>
/// <param name="filePath"></param>
/// <param name="paths"></param>
private static void XlsxToImg(Workbook book, string paths)
{
//string filename = Path.GetFileNameWithoutExtension(filePath).ToLower();
//Workbook book = new Workbook(filePath);
//创建一个图表选项的对象
Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
imgOptions.AllColumnsInOnePagePerSheet = true; //如果AllColumnsInOnePagePerSheet为true,则一个工作表的所有列内容将仅输出到结果中的一个页面。pagesetup的纸张大小宽度无效,pagesetup的其他设置仍然有效
imgOptions.OnePagePerSheet = true; //一个工作表的所有列内容将仅输出到结果中的一个页面
imgOptions.ImageFormat = ImageFormat.Png;
//imgOptions.IsCellAutoFit = true;
int count = book.Worksheets.Count;
for (int i = 0; i < count; i++)
{
//获取一张工作表
Worksheet sheet = book.Worksheets[i];
//创建一个纸张底色渲染对象
var sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.PageCount; j++)
{
var imgpath = paths;
if (!System.IO.File.Exists(imgpath))
{
sr.ToImage(j, imgpath);
}
}
}
}