Ver*_*rno 5 c# asp.net webforms closedxml
我希望有人可以帮助我使用ClosedXML,因为我是Excel新手出口的人,而且从我看到的ClosedXML,文档在某些方面相当有限.
目前我正在将数据放入Datatable,将行格式化为正确的类型并使用正确的布局导出.
当我尝试在每个单元格中导出包含重复公式的一行时,会发生此问题.
我试图将公式简单地添加为一个字符串,然后我可以在文件导出时突出显示并转换,这显然不是理想的.我发现了一个XML类,XLFormula它完全没有文档,但我认为我应该对此做些什么.
目前我已经(注释掉我正在使用的方式XLFormula,试图XLFormula将公式作为字符串传递并设置为每单位的总出价):
dt.Columns.Add("Qty", typeof(int));
dt.Columns.Add("Bid Per Unit GBP", typeof(double));
dt.Columns.Add("Total Bid GBP"); //typeof(XLFormula)
foreach (DataRow dr in dt.Rows)
{
//XLFormula totalBidFormula = new XLFormula();
dr["Qty"] = 1;
dr["Bid Per Unit GBP"] = 0.00;
dr["Total Bid GBP"] = "=[@Qty]*[@[Bid Per Unit GBP]]";
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.如果我想用ClosedXML做的事情是不可能的,请告诉我,如果你可以建议一个替代的XML导出器(即使它已付费),这将有所帮助!
万一有人偶然发现了我处于相同位置的人,那么closedXML中的公式非常简单.
根据我的理解,在添加值之后,必须将公式直接应用于单元格.
我使用一个简单的for循环来获取当前单元格,因为我需要整个列来保存公式
//get all rows in my datatable
int totalRows = dt.Rows.Count;
//set the first that will be filled with data
int currentRow = 1;
//loop through each row.
for(int i = 0; i < totalRows; i++){
//the current cell will be whatever column you wish to format + the current row
string currentCell = "F" + currentRow;
//create your formula
string AdjustedPriceFormula = "=C" + currentRow + "*" + "D" + currentRow;
//apply your formula to the cell.
theSheet.Cells(currentCell).FormulaA1 = AdjustedPriceFormula;
//increment your counters to apply the same data to the following row
currentRow++
Run Code Online (Sandbox Code Playgroud)
这对我来说已经好几次了,并且为多个列/单元添加了多个公式.
希望这有助于某人!