说明:List<SheetInfo>这个对象可以理解为需要重新赋值的单元格集合,麻烦帮忙看看
- /// <summary>
- /// 解析公式上下标
- /// </summary>
- /// <param name="list"></param>
- private void changeExcel(List<SheetInfo> list)
- {
- foreach (SheetInfo item in list)
- {
-
- if (item.Type == SheetEnum.上下标.ToString())//解析公式上下标
- {
- Font ft = fpSpread1.Sheets[0].Cells[item.Cells].Font;
- Color fc = fpSpread1.Sheets[0].Cells[item.Cells].ForeColor;
- RichTextBox rtb = changeRichText(item.Value, ft,fc);
- fpSpread1.Sheets[0].Cells[item.Cells].Renderer = new FarPoint.Win.Spread.CellType.RichTextCellType();
- fpSpread1.Sheets[0].Cells[item.Cells].Value = rtb.Rtf;
- fpSpread1.Sheets[0].Cells[item.Cells].Row.Height = fpSpread1.Sheets[0].Cells[item.Cells].Row.Height + 10;
- EditBaseCellType celltype = fpSpread1.ActiveSheet.Cells[8, 5].CellType as EditBaseCellType;//当然此处cells的坐标我不仅仅只试了8、5,试过好多了,主要试了上下标所在的单元格
- celltype.WordWrap = true;
- //break;
- }
- }
- }
- /// <summary>
- /// 解析公式上下标
- /// value : J=mt<u>2<u>/4π<u>2<u>i<u>2<u> J=mv<d>m<d><u>2<u>/ω<d>0<d><u>2<u>
- /// </summary>
- /// <returns></returns>
- private RichTextBox changeRichText(string value, Font ft, Color fc)
- {
- RichTextBox rtb = new RichTextBox();
- rtb.Font = ft;
- rtb.ForeColor = fc;
- Font ftsmall = new Font(ft.FontFamily,ft.Size/2);
- string[] info_u = Regex.Split(value, "<u>", RegexOptions.IgnoreCase);//value.Split(new char[3]{'<','u','>'});
- for (int i = 0; i < info_u.Length; i=i+2)
- {
- string[] info_d = Regex.Split(info_u[i], "<d>", RegexOptions.IgnoreCase); //info_u[i].Split(new char[3] { '<', 'd', '>' });
- for (int j = 0; j < info_d.Length; j = j + 2)
- {
- // rtb.Font = ft;
- rtb.SelectionCharOffset = 0;
- rtb.SelectedText = info_d[j];
- if (j + 1 < info_d.Length)
- {
- //rtb.Font = ftsmall;
- rtb.SelectionCharOffset = 0 - RichDeep;
- rtb.SelectedText = info_d[j + 1];
-
- }
- }
- if (i + 1 < info_u.Length)
- {
- //rtb.Font = ftsmall;
- rtb.SelectionCharOffset = RichDeep;
- rtb.SelectedText = info_u[i + 1];
- }
- }
- rtb.WordWrap = true;
- return rtb;
- }
复制代码 |