Hi,
It is writely rather slowly into New0.xls by these codes. Where is the problem? I expect to COPY from onegiven Excel file into New0.xls.
It is writely rather slowly into New0.xls by these codes. Where is the problem? I expect to COPY from onegiven Excel file into New0.xls.
var ExcelObj = new Excel.Application(); //Excel.Workbook Book0 = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true); ExcelObj.ScreenUpdating = false; Excel.Worksheet Worksheet0; string Folder0 = Path.GetDirectoryName(openFileDialog1.FileName); string Path0 = Folder0 + "\\New0.xlsx"; string Col0 = "", Row0 = ""; using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, true)) { WorkbookPart workbookPart = doc.WorkbookPart; /*Sheet theSheet = workbookPart.Workbook.Descendants<Sheet>(). Where(s => s.Name == "Sheet Overall").FirstOrDefault();*/ Sheet theSheet; int sheetIndex = 0; DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbookPart.Workbook.Sheets; //foreach (WorksheetPart wsPart in workbookPart.WorksheetParts) foreach (Sheet sheet in sheets) { //WorkbookStylesPart wstylePart = workbookPart.WorkbookStylesPart; //Stylesheet ss = wstylePart.Stylesheet; Boolean b = CreateSpreadsheetWorkbook(Path0, sheet.Name); string sheetPartType = workbookPart.GetPartById(sheet.Id).GetType().Name; using (StreamWriter sw = new StreamWriter(@File1, true, Encoding.Unicode)) { sw.WriteLine("Sheet name is {0},Sheet ID is {1},sheetId is {2},sheetPartType is {3}", sheet.Name, sheet.Id, sheet.SheetId, sheetPartType); } if (sheetPartType != "WorksheetPart") { //Console.WriteLine("{0} is not a worksheet", sheet.Name); using (StreamWriter sw = new StreamWriter(@File1, true, Encoding.Unicode)) { sw.WriteLine("{0} is not a worksheet", sheet.Name); } } else { WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id); WorkbookStylesPart wstylePart = workbookPart.WorkbookStylesPart; Stylesheet ss = wstylePart.Stylesheet; foreach (Cell cell in worksheetPart.Worksheet.Descendants<Cell>()) { if (cell != null) { using (StreamWriter sw = new StreamWriter(@File1, true, Encoding.Unicode)) { if (cell.DataType == null) { sw.WriteLine("Cell reference is {0}; Cell value is {1}", cell.CellReference, cell.InnerText); Col0 = ColumnPart(cell.CellReference); Row0 = RowPart(cell.CellReference); InsertText(doc, sheet.Name, cell.InnerText, Col0, Convert.ToUInt32(Row0)); } else { switch (cell.DataType.Value) { case CellValues.SharedString: var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>() .FirstOrDefault(); if (stringTable != null) { sw.WriteLine("Cell reference is {0}; Cell value is {1}", cell.CellReference, stringTable.SharedStringTable.ElementAt(int.Parse(cell.InnerText)).InnerText); Col0 = ColumnPart(cell.CellReference); Row0 = RowPart(cell.CellReference); InsertText(doc, sheet.Name, cell.InnerText, Col0, Convert.ToUInt32(Row0)); } break; } } } } ... public static void InsertText(SpreadsheetDocument spreadSheet, string SheetName0, string text, string CellRowRef0, uint CellColRef0) { //using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) // Open the document for editing. //Using(SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) //{ // Get the SharedStringTablePart. If it does not exist, create a new one. SharedStringTablePart shareStringPart; if (spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0) { shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First(); } else { shareStringPart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>(); } // Insert the text into the SharedStringTablePart. // int index = InsertSharedStringItem(text, shareStringPart); // Insert a new worksheet. WorksheetPart worksheetPart = InsertWorksheet(spreadSheet.WorkbookPart); // Insert cell A1 into the new worksheet. Cell cell = InsertCellInWorksheet(CellRowRef0, CellColRef0, worksheetPart); // Set the value of cell A1. cell.CellValue = new CellValue(text); cell.DataType = new EnumValue<CellValues>(CellValues.SharedString); // Save the new worksheet. worksheetPart.Worksheet.Save(); //} } // Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet. // If the cell already exists, returns it. private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart) { DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet = worksheetPart.Worksheet; SheetData sheetData = worksheet.GetFirstChild<SheetData>(); string cellReference = columnName + rowIndex; // If the worksheet does not contain a row with the specified row index, insert one. Row row; if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0) { row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First(); } else { row = new Row() { RowIndex = rowIndex }; sheetData.Append(row); } // If there is not a cell with the specified column name, insert one. if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0) { return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First(); } else { // Cells must be in sequential order according to CellReference. Determine where to insert the new cell. Cell refCell = null; foreach (Cell cell in row.Elements<Cell>()) { if (string.Compare(cell.CellReference.Value, cellReference, true) > 0) { refCell = cell; break; } } Cell newCell = new Cell() { CellReference = cellReference }; row.InsertBefore(newCell, refCell); worksheet.Save(); return newCell; } } // Given text and a SharedStringTablePart, creates a SharedStringItem with the specified text // and inserts it into the SharedStringTablePart. If the item already exists, returns its index. private static int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart) { // If the part does not contain a SharedStringTable, create one. if (shareStringPart.SharedStringTable == null) { shareStringPart.SharedStringTable = new SharedStringTable(); } int i = 0; // Iterate through all the items in the SharedStringTable. If the text already exists, return its index. foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>()) { if (item.InnerText == text) { return i; } i++; } // The text does not exist in the part. Create the SharedStringItem and return its index. shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text))); shareStringPart.SharedStringTable.Save(); return i; }
Many Thanks & Best Regards, Hua Min