简体   繁体   English

将文本框添加到Excel工作表

[英]Add a textbox to excel sheet

I'm new to stackoverflow and this is my first post. 我是stackoverflow的新手,这是我的第一篇文章。 I am working on a project where I have select to select data from a Excel sheet and paste it into an another Excel which contains template. 我正在一个项目中,我已选择从一个Excel工作表中选择数据并将其粘贴到另一个包含模板的Excel中。 The data selected is stored in an intermediate XML which is stored in separate excel sheet. 所选数据存储在中间XML中,该XML存储在单独的Excel工作表中。 Since excel cell has got a character limit for 32767 characters so XML is split into substrings of 32767 characters and then each substring is stored in different cells. 由于excel单元格的字符数限制为32767个字符,因此将XML拆分为32767个字符的子字符串,然后将每个子字符串存储在不同的单元格中。 The problem is at the time of reading when all the strings are joined to get the complete xml it is getting corrupted. 问题是在读取所有字符串以获取完整的xml时,它已损坏。 So I thought of textbox as a substitute to store this xml. 因此,我想到了将文本框替换为存储此xml的工具。 Because textbox has fairly large character limit. 因为文本框具有相当大的字符限制。 So I want to add a textbox control to an Excel sheet. 因此,我想将文本框控件添加到Excel工作表中。 I am using C#. 我正在使用C#。 How to do this? 这个怎么做? Thanks in advance. 提前致谢。

Here is the existing code that is being used. 这是正在使用的现有代码。

  private XmlDocument GetXml()
  {
      Excel.Workbook WB = Globals.ThisAddIn.Application.ActiveWorkbook;
      Excel.Worksheet ws = null;
      XmlDocument xmlDoc = new XmlDocument();
      string strXml = string.Empty;
      int XmlColumn = 1;
      try
      {
          ws = WB.Sheets["XML"];
          while (ws.Cells[XmlColumn, 1].Value != null)
          {
              strXml = strXml + ws.Cells[XmlColumn, 1].Value.ToString();
              XmlColumn++;
          }
          if (strXml != "")
              xmlDoc.LoadXml(strXml);
          else
          {
              XmlNode RootNode = xmlDoc.CreateElement("R");
              XmlNode TaggingXmlDocNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
              xmlDoc.AppendChild(TaggingXmlDocNode);

              XmlNode RawExcel = xmlDoc.CreateElement("RawExcel");
              RawExcel.InnerText = Globals.ThisAddIn.Application.ActiveWorkbook.Name;
              RootNode.AppendChild(RawExcel);
              xmlDoc.AppendChild(RootNode);

              ws.Cells[1, 1] = xmlDoc.OuterXml;
          }

      }
      catch
      {
          xmlDoc = null;
      }
      return xmlDoc;
  }

  private void SaveXml(XmlDocument xmlDoc)
  {
      Excel.Workbook WB = Globals.ThisAddIn.Application.ActiveWorkbook;
      Excel.Worksheet ws = null;
      string strXml = string.Empty;
      if (xmlDoc != null)
      {
          strXml = xmlDoc.OuterXml;
          ws = WB.Sheets["XML"];
          int XmlColumn = 1;
          string Temp = strXml;
          for (int Len = 0; Len < strXml.Length; Len = Len + 32700, XmlColumn++)
          {
              if (Temp.Length >= 32700)
              {
                  string s = Temp.Substring(0, 32699);
                  ws.Cells[XmlColumn, 1] = s.Substring(0, s.LastIndexOf('>') + 1);
                  int EndPoint = s.LastIndexOf('>');
                  Temp = Temp.Substring(EndPoint + 1);
              }
              else
              {
                  ws.Cells[XmlColumn, 1] = Temp;
              }
          }
      }
      else
      {
          xmlDoc = new XmlDocument();
          XmlNode RootNode = xmlDoc.CreateElement("R");
          XmlNode TaggingXmlDocNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
          xmlDoc.AppendChild(TaggingXmlDocNode);

          XmlNode RawExcel = xmlDoc.CreateElement("RawExcel");
          RawExcel.InnerText = Globals.ThisAddIn.Application.ActiveWorkbook.Name;
          RootNode.AppendChild(RawExcel);
          xmlDoc.AppendChild(RootNode);

          ws = (Excel.Worksheet)WB.Sheets.Add(After: WB.Sheets[WB.Sheets.Count]);
          ws.Name = "XML";
          ws.Cells[1, 1] = xmlDoc.OuterXml;
          //ws.Visible = Excel.XlSheetVisibility.xlSheetVeryHidden;              
      }
      WB.Save();
  }

I've tried adding a textbox to the sheet and writing the data to it. 我尝试将文本框添加到工作表并将数据写入其中。 But it doesn't help because the textbox on excel sheet has got a character limit for 32767 characters similar to that of an excel cell. 但这无济于事,因为excel工作表上的文本框对32767个字符的字符限制类似于excel单元格的字符限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM