回复 15楼seasky083的帖子
请使用以下代码测试,在设置公式之前把 EnableCrossSheetReference设置为 true:
- public ActionResult Index([FarPoint.Mvc.Spread.MvcSpread(true)]FarPoint.Mvc.Spread.FpSpread test)
- {
- //string str = "'需关闭''帖子'!A19+需解决帖子!A19";
- //Regex r = new Regex(@"('([^']|'')+'|(?<=^|[+\-*\/^&%]).+(?=\!\w+(?:$|[+\-*\/^&%])))");//@"(?<=^|[+\-*\/^&%])('([^']|'')+'|.+?(?=\!\w+(?:$|[+\-*\/^&%])))"
- //MatchCollection ma = r.Matches(str);
- test.Sheets.Clear();
- FpSpread book = new FpSpread();
- book.OpenExcel(this.Server.MapPath("aa.xlsx"));
- int i = 0;
- test.EnableCrossSheetReference = false;//turn off this flag before open excel
- List<Dictionary<string, string>> _SheetListNon = new List<Dictionary<string, string>>();
- foreach (FarPoint.Web.Spread.SheetView sheet in book.Sheets)
- {
- Dictionary<string, string> dicSheetNon = new Dictionary<string, string>();
- FpSpread bookIn = new FpSpread();
- bookIn.Sheets.Add(sheet);
- MemoryStream ms = new MemoryStream();
- bookIn.SaveExcel(ms);
- byte[] b = ms.ToArray();
- FarPoint.Web.Spread.SheetView sv = new FarPoint.Web.Spread.SheetView();
- sv.SheetName = sheet.SheetName;
- test.Sheets.Add(sv);
- sv.OpenExcel(new MemoryStream(b), 0);
- int maxrow = sv.RowCount;
- int maxcol = sv.ColumnCount;
- //这里将对应的excel单元格内容提取出来(以!号开头的)
- for (int k = 0; i < maxrow; i++)
- {
- for (int j = 0; j < maxcol; j++)
- {
- string s = sv.Cells[k, j].Text;
- string formula = sv.Cells[k, j].Formula;
- if (!string.IsNullOrEmpty(s) && string.IsNullOrEmpty(formula))
- {
- if (s[0] == '!')
- {
- dicSheetNon.Add(k + "-" + j, s);
- }
- }
- }
- }
- _SheetListNon.Add(dicSheetNon);
- i++;
- }
- //将对应!公式替换掉
- test.EnableCrossSheetReference = true;//turn off this flag before open excel
- for (int k = 0; k < _SheetListNon.Count; k++)
- {
-
- foreach (KeyValuePair<string, string> cellNon in _SheetListNon[k])
- {
- string[] key = cellNon.Key.Split('-');
- int x = int.Parse(key[0]);
- int y = int.Parse(key[1]);
- try
- {
- //判断能否找到对应的数据
- //就是这个地方找不到对应的源-错误消息:无效的源名称 Error offset: 0
- //test.Sheets[k].Cells[x, y].Formula = cellNon.Value.Substring(1);
- test.Sheets[k].SetFormula(x, y,cellNon.Value.Substring(1));
- }
- catch
- {
- //找不到直接将公式赋值给单元格
- test.Sheets[k].Cells[x, y].Text = cellNon.Value.Substring(1);
- }
- }
- }
- test.Sheets[0].PageSize = 30;
- test.Width = 1200;
- test.Height = 500;
- //test.Sheets[0].Cells[0, 0].Formula = "!'Sheet2'!A1".Substring(1);
- return View();
- }
复制代码 |