public void SimpleTest()
        {
            var cell1 = new SimpleCell("1");
            var cell2 = new SimpleCell("2");
            var cell3 = new FormulaCell("={0}+{1}", cell1, cell2);

            var spreadsheet = new TSVSpreadsheet();
            spreadsheet.AddCell(cell1, 0, 0);
            spreadsheet.AddCell(cell2, 1, 0);
            spreadsheet.AddCell(cell3, 1, 1);

            Assert.AreEqual("1\t2\t\n\t=A1+B1\t\n", spreadsheet.Export());
        }
Esempio n. 2
0
 private static void SetValue(ExcelWorkbook workbook, FormulaCell item, object v)
 {
     if (item.Column == 0)
     {
         if (item.SheetID <= 0)
         {
             workbook.Names[item.Row].NameValue = v;
         }
         else
         {
             var sh = workbook.Worksheets.GetBySheetID(item.SheetID);
             sh.Names[item.Row].NameValue = v;
         }
     }
     else
     {
         var sheet = workbook.Worksheets.GetBySheetID(item.SheetID);
         sheet.SetValueInner(item.Row, item.Column, v);
     }
 }
Esempio n. 3
0
        private static void SetStyle(ExcelWorkbook workbook, FormulaCell item, DataType dataType)
        {
            var sheet = workbook.Worksheets.GetBySheetID(item.SheetID);

            if (dataType == DataType.Date)
            {
                sheet.Cells[item.Row, item.Column].Style.Numberformat.Format = ExcelNumberFormat.GetFromBuildInFromID(14);
            }
            else if (dataType == DataType.Time)
            {
                sheet.Cells[item.Row, item.Column].Style.Numberformat.Format = ExcelNumberFormat.GetFromBuildInFromID(21);
            }
            else if (dataType == DataType.Integer)
            {
                sheet.Cells[item.Row, item.Column].Style.Numberformat.Format = ExcelNumberFormat.GetFromBuildInFromID(1);
            }
            else if (dataType == DataType.Decimal)
            {
                sheet.Cells[item.Row, item.Column].Style.Numberformat.Format = ExcelNumberFormat.GetFromBuildInFromID(2);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// The overload apply method
        /// Creates the Worksheet xml document
        /// </summary>
        /// <param name="bsd">WorkSheetData</param>
        public void Apply(WorkSheetData bsd)
        {
            _xlsContext.CurrentSheet = bsd;
            _writer.WriteStartDocument();
            _writer.WriteStartElement("worksheet", OpenXmlNamespaces.SpreadsheetML);
            //if (bsd.emtpyWorksheet)
            //{
            //    _writer.WriteStartElement("sheetData");
            //    _writer.WriteEndElement();
            //}
            //else
            {
                // default info
                if (bsd.defaultColWidth >= 0 || bsd.defaultRowHeight >= 0)
                {
                    _writer.WriteStartElement("sheetFormatPr");

                    if (bsd.defaultColWidth >= 0)
                    {
                        double colWidht = (double)bsd.defaultColWidth;
                        _writer.WriteAttributeString("defaultColWidth", Convert.ToString(colWidht, CultureInfo.GetCultureInfo("en-US")));
                    }
                    if (bsd.defaultRowHeight >= 0)
                    {
                        TwipsValue tv = new TwipsValue(bsd.defaultRowHeight);
                        _writer.WriteAttributeString("defaultRowHeight", Convert.ToString(tv.ToPoints(), CultureInfo.GetCultureInfo("en-US")));
                    }
                    if (bsd.zeroHeight)
                    {
                        _writer.WriteAttributeString("zeroHeight", "1");
                    }
                    if (bsd.customHeight)
                    {
                        _writer.WriteAttributeString("customHeight", "1");
                    }
                    if (bsd.thickTop)
                    {
                        _writer.WriteAttributeString("thickTop", "1");
                    }
                    if (bsd.thickBottom)
                    {
                        _writer.WriteAttributeString("thickBottom", "1");
                    }

                    _writer.WriteEndElement(); // sheetFormatPr
                }



                // Col info
                if (bsd.colInfoDataTable.Count > 0)
                {
                    _writer.WriteStartElement("cols");
                    foreach (ColumnInfoData col in bsd.colInfoDataTable)
                    {
                        _writer.WriteStartElement("col");
                        // write min and max
                        // booth values are 0 based in the binary format and 1 based in the oxml format
                        // so you have to add 1 to the value!

                        _writer.WriteAttributeString("min", (col.min + 1).ToString());
                        _writer.WriteAttributeString("max", (col.max + 1).ToString());

                        if (col.widht != 0)
                        {
                            double colWidht = (double)col.widht / 256;
                            _writer.WriteAttributeString("width", Convert.ToString(colWidht, CultureInfo.GetCultureInfo("en-US")));
                        }
                        if (col.hidden)
                        {
                            _writer.WriteAttributeString("hidden", "1");
                        }

                        if (col.outlineLevel > 0)
                        {
                            _writer.WriteAttributeString("outlineLevel", col.outlineLevel.ToString());
                        }

                        if (col.customWidth)
                        {
                            _writer.WriteAttributeString("customWidth", "1");
                        }


                        if (col.bestFit)
                        {
                            _writer.WriteAttributeString("bestFit", "1");
                        }

                        if (col.phonetic)
                        {
                            _writer.WriteAttributeString("phonetic", "1");
                        }

                        if (col.style > 15)
                        {
                            _writer.WriteAttributeString("style", Convert.ToString(col.style - this._xlsContext.XlsDoc.WorkBookData.styleData.XFCellStyleDataList.Count, CultureInfo.GetCultureInfo("en-US")));
                        }

                        _writer.WriteEndElement(); // col
                    }


                    _writer.WriteEndElement();
                }
                // End col info

                _writer.WriteStartElement("sheetData");
                //  bsd.rowDataTable.Values
                foreach (RowData row in bsd.rowDataTable.Values)
                {
                    // write row start tag
                    // Row
                    _writer.WriteStartElement("row");
                    // the rowindex from the binary format is zero based, the ooxml format is one based
                    _writer.WriteAttributeString("r", (row.Row + 1).ToString());
                    if (row.height != null)
                    {
                        _writer.WriteAttributeString("ht", Convert.ToString(row.height.ToPoints(), CultureInfo.GetCultureInfo("en-US")));
                        if (row.customHeight)
                        {
                            _writer.WriteAttributeString("customHeight", "1");
                        }
                    }

                    if (row.hidden)
                    {
                        _writer.WriteAttributeString("hidden", "1");
                    }
                    if (row.outlineLevel > 0)
                    {
                        _writer.WriteAttributeString("outlineLevel", row.outlineLevel.ToString());
                    }
                    if (row.collapsed)
                    {
                        _writer.WriteAttributeString("collapsed", "1");
                    }
                    if (row.customFormat)
                    {
                        _writer.WriteAttributeString("customFormat", "1");
                        if (row.style > 15)
                        {
                            _writer.WriteAttributeString("s", (row.style - this._xlsContext.XlsDoc.WorkBookData.styleData.XFCellStyleDataList.Count).ToString());
                        }
                    }
                    if (row.thickBot)
                    {
                        _writer.WriteAttributeString("thickBot", "1");
                    }
                    if (row.thickTop)
                    {
                        _writer.WriteAttributeString("thickTop", "1");
                    }
                    if (row.minSpan + 1 > 0 && row.maxSpan > 0 && row.minSpan + 1 < row.maxSpan)
                    {
                        _writer.WriteAttributeString("spans", (row.minSpan + 1).ToString() + ":" + row.maxSpan.ToString());
                    }

                    row.Cells.Sort();
                    foreach (AbstractCellData cell in row.Cells)
                    {
                        // Col
                        _writer.WriteStartElement("c");
                        _writer.WriteAttributeString("r", ExcelHelperClass.intToABCString((int)cell.Col, (cell.Row + 1).ToString()));

                        if (cell.TemplateID > 15)
                        {
                            _writer.WriteAttributeString("s", (cell.TemplateID - this._xlsContext.XlsDoc.WorkBookData.styleData.XFCellStyleDataList.Count).ToString());
                        }

                        if (cell is StringCell)
                        {
                            _writer.WriteAttributeString("t", "s");
                        }
                        if (cell is FormulaCell)
                        {
                            FormulaCell fcell = (FormulaCell)cell;


                            if (((FormulaCell)cell).calculatedValue is String)
                            {
                                _writer.WriteAttributeString("t", "str");
                            }
                            else if (((FormulaCell)cell).calculatedValue is double)
                            {
                                _writer.WriteAttributeString("t", "n");
                            }
                            else if (((FormulaCell)cell).calculatedValue is byte)
                            {
                                _writer.WriteAttributeString("t", "b");
                            }
                            else if (((FormulaCell)cell).calculatedValue is int)
                            {
                                _writer.WriteAttributeString("t", "e");
                            }


                            // <f>1</f>
                            _writer.WriteStartElement("f");
                            if (!fcell.isSharedFormula)
                            {
                                String value = FormulaInfixMapping.mapFormula(fcell.PtgStack, this._xlsContext);


                                if (fcell.usesArrayRecord)
                                {
                                    _writer.WriteAttributeString("t", "array");
                                    _writer.WriteAttributeString("ref", ExcelHelperClass.intToABCString((int)cell.Col, (cell.Row + 1).ToString()));
                                }
                                if (fcell.alwaysCalculated)
                                {
                                    _writer.WriteAttributeString("ca", "1");
                                }

                                if (value.Equals(""))
                                {
                                    TraceLogger.Debug("Formula Parse Error in Row {0}\t Column {1}\t", cell.Row.ToString(), cell.Col.ToString());
                                }

                                _writer.WriteString(value);
                            }
                            /// If this cell is part of a shared formula
                            ///
                            else
                            {
                                SharedFormulaData sfd = bsd.checkFormulaIsInShared(cell.Row, cell.Col);
                                if (sfd != null)
                                {
                                    // t="shared"
                                    _writer.WriteAttributeString("t", "shared");
                                    //  <f t="shared" ref="C4:C11" si="0">H4+I4-J4</f>
                                    _writer.WriteAttributeString("si", sfd.ID.ToString());
                                    if (sfd.RefCount == 0)
                                    {
                                        /// Write value and reference
                                        _writer.WriteAttributeString("ref", sfd.getOXMLFormatedData());

                                        String value = FormulaInfixMapping.mapFormula(sfd.PtgStack, this._xlsContext, sfd.rwFirst, sfd.colFirst);
                                        _writer.WriteString(value);

                                        sfd.RefCount++;
                                    }
                                }
                                else
                                {
                                    TraceLogger.Debug("Formula Parse Error in Row {0}\t Column {1}\t", cell.Row.ToString(), cell.Col.ToString());
                                }
                            }

                            _writer.WriteEndElement();
                            /// write down calculated value from a formula
                            ///

                            _writer.WriteStartElement("v");

                            if (((FormulaCell)cell).calculatedValue is int)
                            {
                                _writer.WriteString(FormulaInfixMapping.getErrorStringfromCode((int)((FormulaCell)cell).calculatedValue));
                            }
                            else
                            {
                                _writer.WriteString(Convert.ToString(((FormulaCell)cell).calculatedValue, CultureInfo.GetCultureInfo("en-US")));
                            }

                            _writer.WriteEndElement();
                        }
                        else
                        {// Data !!!
                            _writer.WriteElementString("v", cell.getValue());
                        }
                        // add a type to the c element if the formula returns following types

                        _writer.WriteEndElement();  // close cell (c)
                    }


                    _writer.WriteEndElement();  // close row
                }

                // close tags
                _writer.WriteEndElement();      // close sheetData


                // Add the mergecell part
                //
                // - <mergeCells count="2">
                //        <mergeCell ref="B3:C3" />
                //        <mergeCell ref="E3:F4" />
                //     </mergeCells>
                if (bsd.MERGECELLSData != null)
                {
                    _writer.WriteStartElement("mergeCells");
                    _writer.WriteAttributeString("count", bsd.MERGECELLSData.cmcs.ToString());
                    foreach (MergeCellData mcell in bsd.MERGECELLSData.mergeCellDataList)
                    {
                        _writer.WriteStartElement("mergeCell");
                        _writer.WriteAttributeString("ref", mcell.getOXMLFormatedData());
                        _writer.WriteEndElement();
                    }
                    // close mergeCells Tag
                    _writer.WriteEndElement();
                }

                // hyperlinks!

                if (bsd.HyperLinkList.Count != 0)
                {
                    _writer.WriteStartElement("hyperlinks");
                    bool writtenParentElement = false;
                    foreach (HyperlinkData link in bsd.HyperLinkList)
                    {
                        //    Uri url;
                        //    if (link.absolute)
                        //    {

                        //        if (link.url.StartsWith("http", true, CultureInfo.GetCultureInfo("en-US"))
                        //            || link.url.StartsWith("mailto", true, CultureInfo.GetCultureInfo("en-US")))
                        //        {
                        //            url = new Uri(link.url, UriKind.Absolute);

                        //        }
                        //        else
                        //        {
                        //            link.url = "file:///" + link.url;
                        //            url = new Uri(link.url, UriKind.Absolute);
                        //        }

                        //    }
                        //    else
                        //    {

                        //        url = new Uri(link.url, UriKind.Relative);

                        //    }
                        //    try
                        //    {
                        //        if (System.Uri.IsWellFormedUriString(url.LocalPath.ToString(), System.UriKind.Absolute))
                        //        {

                        //if (!writtenParentElement)
                        //{

                        //    writtenParentElement = true;
                        //}
                        string refstring;

                        if (link.colLast == link.colFirst && link.rwLast == link.rwFirst)
                        {
                            refstring = ExcelHelperClass.intToABCString((int)link.colLast, (link.rwLast + 1).ToString());
                        }
                        else
                        {
                            refstring = ExcelHelperClass.intToABCString((int)link.colFirst, (link.rwFirst + 1).ToString()) + ":" + ExcelHelperClass.intToABCString((int)link.colLast, (link.rwLast + 1).ToString());
                        }

                        if (link.url != null)
                        {
                            ExternalRelationship er = this._xlsContext.SpreadDoc.WorkbookPart.GetWorksheetPart().AddExternalRelationship(OpenXmlRelationshipTypes.HyperLink, link.url.Replace(" ", ""));

                            _writer.WriteStartElement("hyperlink");
                            _writer.WriteAttributeString("ref", refstring);
                            _writer.WriteAttributeString("r", "id", OpenXmlNamespaces.Relationships, er.Id.ToString());

                            _writer.WriteEndElement();
                        }
                        else if (link.location != null)
                        {
                            _writer.WriteStartElement("hyperlink");
                            _writer.WriteAttributeString("ref", refstring);
                            _writer.WriteAttributeString("location", link.location);
                            if (link.display != null)
                            {
                                _writer.WriteAttributeString("display", link.display);
                            }
                            _writer.WriteEndElement();
                        }

                        /*           }
                         * }
                         *  catch (Exception ex)
                         *  {
                         *      TraceLogger.DebugInternal(ex.Message.ToString());
                         *      TraceLogger.DebugInternal(ex.StackTrace.ToString());
                         *  }
                         * }*/
                    }
                    _writer.WriteEndElement(); // hyperlinks
                    if (writtenParentElement)
                    {
                    }
                }

                // worksheet margins !!
                if (bsd.leftMargin != null && bsd.topMargin != null &&
                    bsd.rightMargin != null && bsd.bottomMargin != null &&
                    bsd.headerMargin != null && bsd.footerMargin != null)
                {
                    _writer.WriteStartElement("pageMargins");
                    {
                        _writer.WriteAttributeString("left", Convert.ToString(bsd.leftMargin, CultureInfo.GetCultureInfo("en-US")));
                        _writer.WriteAttributeString("right", Convert.ToString(bsd.rightMargin, CultureInfo.GetCultureInfo("en-US")));
                        _writer.WriteAttributeString("top", Convert.ToString(bsd.topMargin, CultureInfo.GetCultureInfo("en-US")));
                        _writer.WriteAttributeString("bottom", Convert.ToString(bsd.bottomMargin, CultureInfo.GetCultureInfo("en-US")));
                        _writer.WriteAttributeString("header", Convert.ToString(bsd.headerMargin, CultureInfo.GetCultureInfo("en-US")));
                        _writer.WriteAttributeString("footer", Convert.ToString(bsd.footerMargin, CultureInfo.GetCultureInfo("en-US")));
                    }
                    _writer.WriteEndElement(); // pageMargins
                }

                // page setup settings
                if (bsd.PageSetup != null)
                {
                    _writer.WriteStartElement("pageSetup");

                    if (!bsd.PageSetup.fNoPls && bsd.PageSetup.iPaperSize > 0 && bsd.PageSetup.iPaperSize < 255)
                    {
                        _writer.WriteAttributeString("paperSize", bsd.PageSetup.iPaperSize.ToString());
                    }
                    if (bsd.PageSetup.iScale >= 10 && bsd.PageSetup.iScale <= 400)
                    {
                        _writer.WriteAttributeString("scale", bsd.PageSetup.iScale.ToString());
                    }
                    _writer.WriteAttributeString("firstPageNumber", bsd.PageSetup.iPageStart.ToString());
                    _writer.WriteAttributeString("fitToWidth", bsd.PageSetup.iFitWidth.ToString());
                    _writer.WriteAttributeString("fitToHeight", bsd.PageSetup.iFitHeight.ToString());

                    if (bsd.PageSetup.fLeftToRight)
                    {
                        _writer.WriteAttributeString("pageOrder", "overThenDown");
                    }

                    if (!bsd.PageSetup.fNoOrient)
                    {
                        if (bsd.PageSetup.fPortrait)
                        {
                            _writer.WriteAttributeString("orientation", "portrait");
                        }
                        else
                        {
                            _writer.WriteAttributeString("orientation", "landscape");
                        }
                    }

                    //10 <attribute name="usePrinterDefaults" type="xsd:boolean" use="optional" default="true"/>

                    if (bsd.PageSetup.fNoColor)
                    {
                        _writer.WriteAttributeString("blackAndWhite", "1");
                    }
                    if (bsd.PageSetup.fDraft)
                    {
                        _writer.WriteAttributeString("draft", "1");
                    }

                    if (bsd.PageSetup.fNotes)
                    {
                        if (bsd.PageSetup.fEndNotes)
                        {
                            _writer.WriteAttributeString("cellComments", "atEnd");
                        }
                        else
                        {
                            _writer.WriteAttributeString("cellComments", "asDisplayed");
                        }
                    }
                    if (bsd.PageSetup.fUsePage)
                    {
                        _writer.WriteAttributeString("useFirstPageNumber", "1");
                    }

                    switch (bsd.PageSetup.iErrors)
                    {
                    case 0x00: _writer.WriteAttributeString("errors", "displayed"); break;

                    case 0x01: _writer.WriteAttributeString("errors", "blank"); break;

                    case 0x02: _writer.WriteAttributeString("errors", "dash"); break;

                    case 0x03: _writer.WriteAttributeString("errors", "NA"); break;

                    default: _writer.WriteAttributeString("errors", "displayed"); break;
                    }

                    _writer.WriteAttributeString("horizontalDpi", bsd.PageSetup.iRes.ToString());
                    _writer.WriteAttributeString("verticalDpi", bsd.PageSetup.iVRes.ToString());
                    if (!bsd.PageSetup.fNoPls)
                    {
                        _writer.WriteAttributeString("copies", bsd.PageSetup.iCopies.ToString());
                    }

                    _writer.WriteEndElement();
                }

                // embedded drawings (charts etc)
                if (bsd.ObjectsSequence != null)
                {
                    _writer.WriteStartElement(Sml.Sheet.ElDrawing, Sml.Ns);
                    {
                        _writer.WriteAttributeString("r", "id", OpenXmlNamespaces.Relationships, this._worksheetPart.DrawingsPart.RelIdToString);
                        bsd.ObjectsSequence.Convert(new DrawingMapping(this._xlsContext, this._worksheetPart.DrawingsPart, false));
                    }
                    _writer.WriteEndElement();
                }
            }

            _writer.WriteEndElement();      // close worksheet
            _writer.WriteEndDocument();

            // close writer
            _writer.Flush();
        }
Esempio n. 5
0
 private void ThenCellIsEvaluatedBefore(FormulaCell before, FormulaCell after)
 {
     Assert.Less(evaluationOrder.IndexOf(before), evaluationOrder.IndexOf(after));
 }
Esempio n. 6
0
 public void WhenFormulaIs(FormulaCell cell, string formula)
 {
     cell.Formula = formula;
 }
Esempio n. 7
0
 public void WhenFormulaIsString(FormulaCell cell, string str)
 {
     cell.Formula = '"' + str + '"';
 }
Esempio n. 8
0
 public void WhenOneCellReferencesAnother(FormulaCell cell, ICell referenced)
 {
     cell.Formula = $"({referenced.Name})";
 }
Esempio n. 9
0
 public void ThenValueIs(FormulaCell cell, float expected)
 {
     Assert.AreEqual(expected, (float)cell.Value);
 }
Esempio n. 10
0
 public void ThenValueIs(FormulaCell cell, string expected)
 {
     Assert.AreEqual(expected, (string)cell.Value);
 }
Esempio n. 11
0
 public void ThenFormulaIs(FormulaCell cell, string formula)
 {
     Assert.AreEqual(formula, cell.Formula);
 }
Esempio n. 12
0
        /**
         * Constructor
         *
         * @param w The workbook to interrogate
         * @param out The output stream to which the CSV values are written
         * @param encoding The encoding used by the output stream.  Null or
         * unrecognized values cause the encoding to default to UTF8
         * @exception java.io.IOException
         */
        public Formulas(Workbook w, TextWriter os, string encoding)
        {
            if (encoding == null || encoding != "UnicodeBig")
            {
                encoding = "UTF8";
            }

            try
            {
                //OutputStreamWriter osw = new OutputStreamWriter(out, encoding);
                //BufferedWriter os = new BufferedWriter(osw);

                ArrayList parseErrors = new ArrayList();

                for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
                {
                    Sheet s = w.getSheet(sheet);

                    os.Write(s.getName());
                    os.WriteLine();

                    Cell[] row = null;
                    Cell   c   = null;

                    for (int i = 0; i < s.getRows(); i++)
                    {
                        row = s.getRow(i);

                        for (int j = 0; j < row.Length; j++)
                        {
                            c = row[j];
                            if (c.getType() == CellType.NUMBER_FORMULA ||
                                c.getType() == CellType.STRING_FORMULA ||
                                c.getType() == CellType.BOOLEAN_FORMULA ||
                                c.getType() == CellType.DATE_FORMULA ||
                                c.getType() == CellType.FORMULA_ERROR)
                            {
                                FormulaCell   nfc = (FormulaCell)c;
                                StringBuilder sb  = new StringBuilder();
                                CSharpJExcel.Jxl.Biff.CellReferenceHelper.getCellReference(c.getColumn(), c.getRow(), sb);

                                try
                                {
                                    os.Write("Formula in " + sb.ToString() +
                                             " value:  " + c.getContents());
                                    os.Flush();
                                    os.Write(" formula: " + nfc.getFormula());
                                    os.Flush();
                                    os.WriteLine();
                                }
                                catch (FormulaException e)
                                {
                                    os.WriteLine();
                                    parseErrors.Add(s.getName() + '!' + sb.ToString() + ": " + e);
                                }
                            }
                        }
                    }
                }
                os.Flush();
//				os.close();

                if (parseErrors.Count > 0)
                {
                    Console.WriteLine();
                    Console.WriteLine("There were " + parseErrors.Count + " errors");

                    foreach (object s in parseErrors)
                    {
                        Console.WriteLine(s.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }