Example #1
0
        /**
         * Set a string value for the cell.
         *
         * @param str  value to Set the cell to.  For formulas we'll Set the 'pre-Evaluated result string,
         * for String cells we'll Set its value.  For other types we will
         * change the cell to a string cell and Set its value.
         * If value is null then we will change the cell to a Blank cell.
         */
        public void SetCellValue(IRichTextString str)
        {
            if (str == null || str.String == null)
            {
                SetCellType(CellType.BLANK);
                return;
            }
            CellType cellType = CellType;

            switch (cellType)
            {
            case CellType.FORMULA:
                _cell.v = (str.String);
                _cell.t = (ST_CellType.str);
                break;

            default:
                if (_cell.t == ST_CellType.inlineStr)
                {
                    //set the 'pre-Evaluated result
                    _cell.v = str.String;
                }
                else
                {
                    _cell.t = ST_CellType.s;
                    XSSFRichTextString rt = (XSSFRichTextString)str;
                    rt.SetStylesTableReference(_stylesSource);
                    int sRef = _sharedStringSource.AddEntry(rt.GetCTRst());
                    _cell.v = sRef.ToString();
                }
                break;
            }
        }
Example #2
0
        public void SetText(XSSFRichTextString str)
        {
            XSSFWorkbook parent = (XSSFWorkbook)this.GetDrawing().GetParent().GetParent();

            str.SetStylesTableReference(parent.GetStylesSource());
            CT_TextParagraph ctTextParagraph = new CT_TextParagraph();

            if (str.NumFormattingRuns == 0)
            {
                CT_RegularTextRun          ctRegularTextRun    = ctTextParagraph.AddNewR();
                CT_TextCharacterProperties characterProperties = ctRegularTextRun.AddNewRPr();
                characterProperties.lang = "en-US";
                characterProperties.sz   = 1100;
                ctRegularTextRun.t       = str.String;
            }
            else
            {
                for (int index = 0; index < str.GetCTRst().sizeOfRArray(); ++index)
                {
                    CT_RElt                    rarray           = str.GetCTRst().GetRArray(index);
                    CT_RPrElt                  pr               = rarray.rPr ?? rarray.AddNewRPr();
                    CT_RegularTextRun          ctRegularTextRun = ctTextParagraph.AddNewR();
                    CT_TextCharacterProperties rPr              = ctRegularTextRun.AddNewRPr();
                    rPr.lang = "en-US";
                    XSSFSimpleShape.ApplyAttributes(pr, rPr);
                    ctRegularTextRun.t = rarray.t;
                }
            }
            this.ctShape.txBody.SetPArray(new CT_TextParagraph[1]
            {
                ctTextParagraph
            });
        }
Example #3
0
        /**
         * Creates a new XSSFRichTextString for you.
         */
        public IRichTextString CreateRichTextString(String text)
        {
            XSSFRichTextString rt = new XSSFRichTextString(text);

            rt.SetStylesTableReference(workbook.GetStylesSource());
            return(rt);
        }
Example #4
0
        public IRichTextString CreateRichTextString(string text)
        {
            XSSFRichTextString xssfRichTextString = new XSSFRichTextString(text);

            xssfRichTextString.SetStylesTableReference(this.workbook.GetStylesSource());
            return((IRichTextString)xssfRichTextString);
        }
Example #5
0
        /**
         * Set a single paragraph of text on the shape. Note this will replace all existing paragraphs Created on the shape.
         * @param str	rich text string representing the paragraph text
         */
        public void SetText(XSSFRichTextString str)
        {
            XSSFWorkbook wb = (XSSFWorkbook)GetDrawing().GetParent().GetParent();

            str.SetStylesTableReference(wb.GetStylesSource());

            CT_TextParagraph p = new CT_TextParagraph();

            if (str.NumFormattingRuns == 0)
            {
                CT_RegularTextRun          r   = p.AddNewR();
                CT_TextCharacterProperties rPr = r.AddNewRPr();
                rPr.lang = (/*setter*/ "en-US");
                rPr.sz   = (/*setter*/ 1100);
                r.t      = (/*setter*/ str.String);
            }
            else
            {
                for (int i = 0; i < str.GetCTRst().SizeOfRArray(); i++)
                {
                    CT_RElt   lt   = str.GetCTRst().GetRArray(i);
                    CT_RPrElt ltPr = lt.rPr;
                    if (ltPr == null)
                    {
                        ltPr = lt.AddNewRPr();
                    }

                    CT_RegularTextRun          r   = p.AddNewR();
                    CT_TextCharacterProperties rPr = r.AddNewRPr();
                    rPr.lang = (/*setter*/ "en-US");

                    ApplyAttributes(ltPr, rPr);

                    r.t = (/*setter*/ lt.t);
                }
            }

            ClearText();
            ctShape.txBody.SetPArray(new CT_TextParagraph[] { p });
            _paragraphs.Add(new XSSFTextParagraph(ctShape.txBody.GetPArray(0), ctShape));
        }
Example #6
0
        public void TestApplyFontWithStyles()
        {
            XSSFRichTextString rt = new XSSFRichTextString("Apache POI");

            StylesTable tbl = new StylesTable();

            rt.SetStylesTableReference(tbl);

            try
            {
                rt.ApplyFont(0, 10, (short)1);
                Assert.Fail("Fails without styles in the table");
            }
            catch (ArgumentOutOfRangeException e)
            {
                // expected
            }

            tbl.PutFont(new XSSFFont());
            rt.ApplyFont(0, 10, (short)1);
            rt.ApplyFont((short)1);
        }
Example #7
0
        /**
         * Set a string value for the cell.
         *
         * @param str  value to Set the cell to.  For formulas we'll Set the 'pre-Evaluated result string,
         * for String cells we'll Set its value.  For other types we will
         * change the cell to a string cell and Set its value.
         * If value is null then we will change the cell to a Blank cell.
         */
        public void SetCellValue(IRichTextString str)
        {
            if (str == null || string.IsNullOrEmpty(str.String))
            {
                SetCellType(CellType.Blank);
                return;
            }

            if (str.Length > SpreadsheetVersion.EXCEL2007.MaxTextLength)
            {
                throw new ArgumentException("The maximum length of cell contents (text) is 32,767 characters");
            }
            CellType cellType = CellType;

            switch (cellType)
            {
            case CellType.Formula:
                _cell.v = (str.String);
                _cell.t = (ST_CellType.str);
                break;

            default:
                if (_cell.t == ST_CellType.inlineStr)
                {
                    //set the 'pre-Evaluated result
                    _cell.v = str.String;
                }
                else
                {
                    _cell.t = ST_CellType.s;
                    XSSFRichTextString rt = (XSSFRichTextString)str;
                    rt.SetStylesTableReference(_stylesSource);
                    int sRef = _sharedStringSource.AddEntry(rt.GetCTRst());
                    _cell.v = sRef.ToString();
                }
                break;
            }
        }
Example #8
0
 public void SetCellValue(IRichTextString str)
 {
     if (str == null || str.String == null)
     {
         this.SetCellType(CellType.BLANK);
     }
     else if (this.CellType == CellType.FORMULA)
     {
         this._cell.v = str.String;
         this._cell.t = ST_CellType.str;
     }
     else if (this._cell.t == ST_CellType.inlineStr)
     {
         this._cell.v = str.String;
     }
     else
     {
         this._cell.t = ST_CellType.s;
         XSSFRichTextString xssfRichTextString = (XSSFRichTextString)str;
         xssfRichTextString.SetStylesTableReference(this._stylesSource);
         this._cell.v = this._sharedStringSource.AddEntry(xssfRichTextString.GetCTRst()).ToString();
     }
 }
Example #9
0
        /// <summary>
        /// Set the cells type (numeric, formula or string)
        /// </summary>
        /// <param name="cellType"></param>
        public void SetCellType(CellType cellType)
        {
            CellType prevType = CellType;

            if (IsPartOfArrayFormulaGroup)
            {
                NotifyArrayFormulaChanging();
            }
            if (prevType == CellType.Formula && cellType != CellType.Formula)
            {
                ((XSSFWorkbook)Sheet.Workbook).OnDeleteFormula(this);
            }

            switch (cellType)
            {
            case CellType.Blank:
                SetBlank();
                break;

            case CellType.Boolean:
                String newVal = ConvertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
                _cell.t = (ST_CellType.b);
                _cell.v = (newVal);
                break;

            case CellType.Numeric:
                _cell.t = (ST_CellType.n);
                break;

            case CellType.Error:
                _cell.t = (ST_CellType.e);
                break;

            case CellType.String:
                if (prevType != CellType.String)
                {
                    String             str = ConvertCellValueToString();
                    XSSFRichTextString rt  = new XSSFRichTextString(str);
                    rt.SetStylesTableReference(_stylesSource);
                    int sRef = _sharedStringSource.AddEntry(rt.GetCTRst());
                    _cell.v = sRef.ToString();
                }
                _cell.t = (ST_CellType.s);
                break;

            case CellType.Formula:
                if (!_cell.IsSetF())
                {
                    CT_CellFormula f = new CT_CellFormula();
                    f.Value = "0";
                    _cell.f = (f);
                    if (_cell.IsSetT())
                    {
                        _cell.unsetT();
                    }
                }
                break;

            default:
                throw new ArgumentException("Illegal cell type: " + cellType);
            }
            if (cellType != CellType.Formula && _cell.IsSetF())
            {
                _cell.unsetF();
            }
        }
Example #10
0
        public void SetCellType(CellType cellType)
        {
            CellType cellType1 = this.CellType;

            if (this.IsPartOfArrayFormulaGroup)
            {
                this.NotifyArrayFormulaChanging();
            }
            if (cellType1 == CellType.FORMULA && cellType != CellType.FORMULA)
            {
                ((XSSFWorkbook)this.Sheet.Workbook).OnDeleteFormula(this);
            }
            switch (cellType)
            {
            case CellType.NUMERIC:
                this._cell.t = ST_CellType.n;
                break;

            case CellType.STRING:
                if (cellType1 != CellType.STRING)
                {
                    XSSFRichTextString xssfRichTextString = new XSSFRichTextString(this.ConvertCellValueToString());
                    xssfRichTextString.SetStylesTableReference(this._stylesSource);
                    this._cell.v = this._sharedStringSource.AddEntry(xssfRichTextString.GetCTRst()).ToString();
                }
                this._cell.t = ST_CellType.s;
                break;

            case CellType.FORMULA:
                if (!this._cell.IsSetF())
                {
                    this._cell.f = new CT_CellFormula()
                    {
                        Value = "0"
                    };
                    if (this._cell.IsSetT())
                    {
                        this._cell.unsetT();
                        break;
                    }
                    break;
                }
                break;

            case CellType.BLANK:
                this.SetBlank();
                break;

            case CellType.BOOLEAN:
                string str = this.ConvertCellValueToBoolean() ? XSSFCell.TRUE_AS_STRING : XSSFCell.FALSE_AS_STRING;
                this._cell.t = ST_CellType.b;
                this._cell.v = str;
                break;

            case CellType.ERROR:
                this._cell.t = ST_CellType.e;
                break;

            default:
                throw new ArgumentException("Illegal cell type: " + (object)cellType);
            }
            if (cellType == CellType.FORMULA || !this._cell.IsSetF())
            {
                return;
            }
            this._cell.unsetF();
        }
Example #11
0
        public void SetText(XSSFRichTextString str)
        {

            XSSFWorkbook wb = (XSSFWorkbook)GetDrawing().GetParent().GetParent();
            str.SetStylesTableReference(wb.GetStylesSource());

            CT_TextParagraph p = new CT_TextParagraph();
            if (str.NumFormattingRuns == 0)
            {
                CT_RegularTextRun r = p.AddNewR();
                CT_TextCharacterProperties rPr = r.AddNewRPr();
                rPr.lang = ("en-US");
                rPr.sz = (1100);
                r.t = str.String;

            }
            else
            {
                for (int i = 0; i < str.GetCTRst().sizeOfRArray(); i++)
                {
                    CT_RElt lt = str.GetCTRst().GetRArray(i);
                    CT_RPrElt ltPr = lt.rPr;
                    if (ltPr == null) ltPr = lt.AddNewRPr();

                    CT_RegularTextRun r = p.AddNewR();
                    CT_TextCharacterProperties rPr = r.AddNewRPr();
                    rPr.lang = ("en-US");

                    ApplyAttributes(ltPr, rPr);

                    r.t = (lt.t);
                }
            }
            ctShape.txBody.SetPArray(new CT_TextParagraph[] { p });

        }
Example #12
0
        /// <summary>
        /// Set the cells type (numeric, formula or string)
        /// </summary>
        /// <param name="cellType"></param>
        public void SetCellType(CellType cellType)
        {
            CellType prevType = CellType;

            if (IsPartOfArrayFormulaGroup)
            {
                NotifyArrayFormulaChanging();
            }
            if (prevType == CellType.Formula && cellType != CellType.Formula)
            {
                ((XSSFWorkbook)Sheet.Workbook).OnDeleteFormula(this);
            }

            switch (cellType)
            {
                case CellType.Blank:
                    SetBlank();
                    break;
                case CellType.Boolean:
                    String newVal = ConvertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING;
                    _cell.t= (ST_CellType.b);
                    _cell.v= (newVal);
                    break;
                case CellType.Numeric:
                    _cell.t = (ST_CellType.n);
                    break;
                case CellType.Error:
                    _cell.t = (ST_CellType.e);
                    break;
                case CellType.String:
                    if (prevType != CellType.String)
                    {
                        String str = ConvertCellValueToString();
                        XSSFRichTextString rt = new XSSFRichTextString(str);
                        rt.SetStylesTableReference(_stylesSource);
                        int sRef = _sharedStringSource.AddEntry(rt.GetCTRst());
                        _cell.v= sRef.ToString();
                    }
                    _cell.t= (ST_CellType.s);
                    break;
                case CellType.Formula:
                    if (!_cell.IsSetF())
                    {
                        CT_CellFormula f = new CT_CellFormula();
                        f.Value = "0";
                        _cell.f = (f);
                        if (_cell.IsSetT()) _cell.unsetT();
                    }
                    break;
                default:
                    throw new ArgumentException("Illegal cell type: " + cellType);
            }
            if (cellType != CellType.Formula && _cell.IsSetF())
            {
                _cell.unsetF();
            }
        }
        public void TestApplyFontWithStyles()
        {
            XSSFRichTextString rt = new XSSFRichTextString("Apache POI");

            StylesTable tbl = new StylesTable();
            rt.SetStylesTableReference(tbl);

            try
            {
                rt.ApplyFont(0, 10, (short)1);
                Assert.Fail("Fails without styles in the table");
            }
            catch (ArgumentOutOfRangeException e)
            {
                // expected
            }

            tbl.PutFont(new XSSFFont());
            rt.ApplyFont(0, 10, (short)1);
            rt.ApplyFont((short)1);
        }
Example #14
0
 /**
  * Creates a new XSSFRichTextString for you.
  */
 public IRichTextString CreateRichTextString(String text)
 {
     XSSFRichTextString rt = new XSSFRichTextString(text);
     rt.SetStylesTableReference(workbook.GetStylesSource());
     return rt;
 }