private void testChemicalListToolStripMenuItem_Click(object sender, EventArgs e) { List <DSSToxChemicals> chemicals = new List <DSSToxChemicals>(); // Open the document for editing. string fileName = documentPath + "\\DSSTox_ToxCastRelease_20151019.xlsx"; using (DocumentFormat.OpenXml.Packaging.SpreadsheetDocument document = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(fileName, false)) { DocumentFormat.OpenXml.Packaging.WorkbookPart wbPart = document.WorkbookPart; DocumentFormat.OpenXml.Packaging.WorksheetPart wsPart = wbPart.WorksheetParts.First(); DocumentFormat.OpenXml.Spreadsheet.SheetData sheetData = wsPart.Worksheet.Elements <DocumentFormat.OpenXml.Spreadsheet.SheetData>().First(); string text = string.Empty; bool first = true; foreach (DocumentFormat.OpenXml.Spreadsheet.Row r in sheetData.Elements <DocumentFormat.OpenXml.Spreadsheet.Row>()) { if (!first) { foreach (DocumentFormat.OpenXml.Spreadsheet.Cell c in r.Elements <DocumentFormat.OpenXml.Spreadsheet.Cell>()) { text = text + this.GetExcelCellValue(c, wbPart) + '\t'; } chemicals.Add(new DSSToxChemicals(text)); } first = false; text = string.Empty; } document.Close(); } foreach (DSSToxChemicals chem in chemicals) { if (!string.IsNullOrEmpty(chem.Structure_SMILES)) { ChemInfo.Molecule mol = new ChemInfo.Molecule(chem.Structure_SMILES); if (mol != null) { foreach (ChemInfo.FunctionalGroup f in this.fGroups) { if ((f.Name != "ESTER-SULFIDE") || (f.Name != "KETENIMINE")) { mol.FindFunctionalGroup(f); } } } chem.AddFunctionalGroups(mol.FunctionalGroups); } } fileName = documentPath + "\\chemicals.json"; System.IO.File.WriteAllText(fileName, Newtonsoft.Json.JsonConvert.SerializeObject(chemicals, Newtonsoft.Json.Formatting.Indented)); }
private void Button1_Click(object sender, EventArgs e) { // Open the document for editing. using (DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheetDocument = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(@"C:\Users\fmogh\OneDrive\Desktop\Excel\Farzad.xlsx", false)) { DocumentFormat.OpenXml.Packaging.WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; DocumentFormat.OpenXml.Packaging.WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); DocumentFormat.OpenXml.Spreadsheet.SheetData sheetData = worksheetPart.Worksheet.Elements <DocumentFormat.OpenXml.Spreadsheet.SheetData>().First(); string text; foreach (DocumentFormat.OpenXml.Spreadsheet.Row r in sheetData.Elements <DocumentFormat.OpenXml.Spreadsheet.Row>()) { foreach (DocumentFormat.OpenXml.Spreadsheet.Cell c in r.Elements <DocumentFormat.OpenXml.Spreadsheet.Cell>()) { text = c.CellValue.Text; Console.Write(text + " "); } } } }
// The DOM approach. // Note that the code below works only for cells that contain numeric values. // static void ReadExcelFileDOM(string fileName) { using (DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheetDocument = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(fileName, false)) { DocumentFormat.OpenXml.Packaging.WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; DocumentFormat.OpenXml.Packaging.WorksheetPart worksheetPart = System.Linq.Enumerable.First(workbookPart.WorksheetParts); DocumentFormat.OpenXml.Spreadsheet.SheetData sheetData = System.Linq.Enumerable.First(worksheetPart.Worksheet.Elements <DocumentFormat.OpenXml.Spreadsheet.SheetData>()); string text; foreach (DocumentFormat.OpenXml.Spreadsheet.Row r in sheetData.Elements <DocumentFormat.OpenXml.Spreadsheet.Row>()) { foreach (DocumentFormat.OpenXml.Spreadsheet.Cell c in r.Elements <DocumentFormat.OpenXml.Spreadsheet.Cell>()) { text = c.CellValue.Text; System.Console.Write(text + " "); } } System.Console.WriteLine(); System.Console.ReadKey(); } }
// The SAX approach. static void ReadExcelFileSAX(string fileName) { using (DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheetDocument = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(fileName, false)) { DocumentFormat.OpenXml.Packaging.WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; DocumentFormat.OpenXml.Packaging.WorksheetPart worksheetPart = System.Linq.Enumerable.First(workbookPart.WorksheetParts); DocumentFormat.OpenXml.OpenXmlReader reader = DocumentFormat.OpenXml.OpenXmlReader.Create(worksheetPart); string text; while (reader.Read()) { if (reader.ElementType == typeof(DocumentFormat.OpenXml.Spreadsheet.CellValue)) { text = reader.GetText(); System.Console.Write(text + " "); } } System.Console.WriteLine(); System.Console.ReadKey(); } }