public SpreadsheetView()
            : base()
        {
            SpreadsheetModel = new SpreadsheetModel(new CellCollection());            
            Dock = DockStyle.Fill;
            CellMouseDoubleClick += new DataGridViewCellMouseEventHandler(SpreadsheetView_CellMouseDoubleClick);
            CellMouseClick += new DataGridViewCellMouseEventHandler(SpreadsheetView_CellMouseClick);
            RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(SpreadsheetView_RowHeaderMouseClick);
            RowHeightChanged += new DataGridViewRowEventHandler(SpreadsheetView_RowHeightChanged);
            ColumnWidthChanged += new DataGridViewColumnEventHandler(SpreadsheetView_ColumnWidthChanged);
            KeyDown += new KeyEventHandler(SpreadsheetView_KeyDown);
            KeyUp += new KeyEventHandler(SpreadsheetView_KeyUp);
            ParentChanged += delegate
            {
                Columns.Clear();

                for (int k = 0; k < ColumnCount; k++)
                {
                    Columns.Add(MakeColumnLabel(k), MakeColumnLabel(k));
                    Columns[k].SortMode = DataGridViewColumnSortMode.NotSortable;
                }

                Rows.Add(RowCount);

                AllowUserToOrderColumns = false;
                SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
            };

            CellEndEdit += new DataGridViewCellEventHandler(SpreadsheetView_CellEndEdit);
            CellBeginEdit += new DataGridViewCellCancelEventHandler(SpreadsheetView_CellBeginEdit);
            RowsRemoved += new DataGridViewRowsRemovedEventHandler(SpreadsheetView_RowsRemoved);

            this.DefaultCellStyle.Font = new Font("Times", 12);
        }
예제 #2
0
		public void ExecuteInsertWorksheet(SpreadsheetModel spreadsheetModel)
		{
			System.Windows.Forms.TabPage newWSPage = new TabPage("Worksheet " +
					(MainForm.WorksheetsTabControl.Controls.Count + 1));

			MainForm.WorksheetsTabControl.Controls.Add(newWSPage);
			newWSPage.Location = new System.Drawing.Point(4, 22);
			newWSPage.Padding = new System.Windows.Forms.Padding(3);
			newWSPage.Size = mainForm.Size;
			newWSPage.TabIndex = 0;
			newWSPage.UseVisualStyleBackColor = true;
			SpreadsheetUserControl ws = new SpreadsheetUserControl();
			ws.Spreadsheet.SpreadsheetModel = spreadsheetModel;
			ws.Spreadsheet.KeyDown += new KeyEventHandler(SpreadsheetView_KeyDown);
			ws.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
									| System.Windows.Forms.AnchorStyles.Left)
									| System.Windows.Forms.AnchorStyles.Right)));
			ws.Location = new System.Drawing.Point(0, 0);
			ws.Name = "spreadsheetUserControl" +
					(MainForm.WorksheetsTabControl.Controls.Count + 1);
			ws.Size = newWSPage.Size;
			newWSPage.Controls.Add(ws);
			ws.Spreadsheet.RefreshView();
		}
 public SpreadsheetControl()
 {
     m = new SpreadsheetModel(new CellCollection());
     InvalidCells = new Queue<CellKey>();
 }
예제 #4
0
 public bool AddSpreadsheet(SpreadsheetModel sheet)
 {
   book.Add(sheet);
   return true;
 }
예제 #5
0
      private bool WriteBook()
      {
          Cell theCell = new Cell();
          Cell defaultCell = new Cell();

          Cell tempCell = new Cell();

          /**** TEST Code*/

          CellCollection cc = new CellCollection();
          FontFamily blahFamily = new FontFamily("Arial");
          Font blahFont = new Font(blahFamily, 13);
          CellFormat blahFormat = new CellFormat(blahFont,Color.Orange, Color.Green);
          Font f = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold);
          f = new Font(f, f.Style | FontStyle.Italic);
          CellFormat blahFormat2 = new CellFormat(f, Color.Black, Color.Black);
          //blahFormat.CellColor = Color.Yellow;
          //blahFormat.TextColor = Color.Purple;
          //tempCell.CellFormat = blahFormat;

          cc[0, 0] = new Cell("= SUM()", blahFormat);
          cc[0, 0].Value = "42";
          cc[0, 1] = new Cell("", tempCell.CellFormat);
          cc[0, 2] = new Cell("", tempCell.CellFormat);
          cc[1, 0] = new Cell("", tempCell.CellFormat);
          cc[1, 0].Value = "blah";
          cc[1, 1] = new Cell("= AVG()", tempCell.CellFormat);
          cc[1, 1].Value = "47";
          cc[1, 2] = new Cell("", blahFormat2);
          cc[1, 2].Value = "99";
          cc[2, 0] = new Cell("", tempCell.CellFormat);
          cc[2, 0].Value = "YourMother";
          cc[2, 1] = new Cell("", tempCell.CellFormat);
          cc[2, 2] = new Cell("", tempCell.CellFormat);

          book[0] = new SpreadsheetModel(cc);
          //Cell theCell = new Cell();

          //END TEST CODE

          double tempDouble = new double();
          bool blank = false;
          fileStream = new FileStream(filename, FileMode.Create);
          textWriter = new XmlTextWriter(fileStream, Encoding.Unicode);

          try
          {

              textWriter.WriteStartElement(filename.Substring(filename.LastIndexOf("\\") + 1,
                                           filename.LastIndexOf(".") - filename.LastIndexOf("\\") - 1));
              textWriter.WriteStartElement("sheet");
              textWriter.WriteStartElement("author");
              textWriter.WriteAttributeString("name", "Nathan Benjamin");
              textWriter.WriteEndElement();

              textWriter.WriteString(Environment.NewLine + "other metadata" + Environment.NewLine);

              int bookRows = book[0].Cells.Rows;
              int bookColumns = book[0].Cells.Columns;

              for (int column = 0; column < bookColumns; column++)
              {
                  textWriter.WriteStartElement("column");
                  textWriter.WriteAttributeString("index", column.ToString());


                  for (int row = 0; row < bookRows; row++)
                  {
                      theCell = book[0].Cells[row, column];
                      /*IS CELL DEFAULT CHECK*/
                      blank = false;
                      if (theCell.Value == null) { blank = true; }
                      else if (theCell.Value.Equals("")) { blank = true; }
                      /*CELL FORMAT needs to be instantiated*/
                      //if (theCell.CellFormat.IsDefault == false) { blank = true; }

                      if (blank == true)
                      {//skip writing the row, go on to the next cell.
                      }
                      else
                      {
                          textWriter.WriteStartElement("row");
                          textWriter.WriteAttributeString("index", row.ToString());

                          if (theCell != null)
                          {
                              /*CONTENT TYPE CHECK*/
                              textWriter.WriteStartElement("content");
                              if (theCell.Formula.Contains("="))
                              {
                                  textWriter.WriteAttributeString("type", "Formula");
                              }
                              else
                              {
                                  try
                                  {
                                      tempDouble = double.Parse(theCell.Value);
                                      textWriter.WriteAttributeString("type", "Number");
                                  }
                                  catch (System.FormatException e)
                                  {
                                      textWriter.WriteAttributeString("type", "Text");
                                  }
                              }

                              
                              /*IF THE CELLFORMAT IS DEFAULT, WE WRITE NOTHING ABOUT CELL FORMAT
                              if(theCell.CellFormat.IsDefault){//skip the cell
                              }
                              else{*/
                                  //Each Format attribute is compared to the default cell. If that .Equals method returns false
                                  //Then theCell has an attribute different from defaultCell, and that attribute must be written.
                                  //CellColor
                                  if (theCell.CellFormat.CellColor.Equals(defaultCell.CellFormat.CellColor) == false)
                                      textWriter.WriteAttributeString("CellColor", theCell.CellFormat.CellColor.ToString());
                                  //TextColor
                                  if (theCell.CellFormat.TextColor.Equals(defaultCell.CellFormat.TextColor) == false)
                                      textWriter.WriteAttributeString("TextColor", theCell.CellFormat.TextColor.ToString());
                                  //Bold
                                  if (theCell.CellFormat.CellFont.Bold.Equals(defaultCell.CellFormat.CellFont.Bold) == false)
                                      textWriter.WriteAttributeString("Bold", "true");
                                  //Italics
                                  if (theCell.CellFormat.CellFont.Italic.Equals(defaultCell.CellFormat.CellFont.Italic) == false)
                                      textWriter.WriteAttributeString("Italic", "true");
                                  //Underline
                                  if (theCell.CellFormat.CellFont.Underline.Equals(defaultCell.CellFormat.CellFont.Underline) == false)
                                      textWriter.WriteAttributeString("Underline", "true");
                                  //FontFamily
                                  if (theCell.CellFormat.CellFont.FontFamily.Equals(defaultCell.CellFormat.CellFont.FontFamily) == false)
                                      textWriter.WriteAttributeString("FontFamily", theCell.CellFormat.CellFont.FontFamily.ToString());
                                  //Size
                                  if (theCell.CellFormat.CellFont.Size.Equals(defaultCell.CellFormat.CellFont.Size) == false)
                                      textWriter.WriteAttributeString("Size", theCell.CellFormat.CellFont.Size.ToString());
                              /*}//end of Format Check
                              */

                              //FORMULA CHECK
                              if (theCell.Formula.Equals("") || theCell.Formula.Equals(null))
                              {} //ignore
                              else{
                                  textWriter.WriteElementString("Formula", theCell.Formula.ToString());
                              }
                              //VALUE CHECK
                              if (theCell.Value.Equals("") || theCell.Value.Equals(null))
                              { } //ignore
                              else
                              {
                                  textWriter.WriteElementString("Value", theCell.Value.ToString());
                              }
                              textWriter.WriteEndElement();//end of content type element?
                          }//end of theCell != null if
                          /*****Where do all these other write elements correspond to?*/

                          textWriter.WriteEndElement();
                      }//end of blank cell check if/else
                      
                  }//end of row loop
                  textWriter.WriteEndElement();
              }//end of column loop
              textWriter.WriteEndElement();
              textWriter.WriteEndElement();
              textWriter.Flush();
          }
          catch (Exception e)
          {
              MessageBox.Show("Error: (" + e.GetType().ToString() + "): " +
                              e.Message + Environment.NewLine +
                              "Debug Data: " + e.ToString(), "Error");
          }
          finally
          {
              if (textWriter != null)
              {
                  textWriter.Close();
                  textWriter = null;
              }
          }
          return true;
      }