private void button3_Click(object sender, EventArgs e) { ReflectOffice.Excel.Application app = new ReflectOffice.Excel.Application(); //app.DisplayAlerts = false; Workbook book = app.Workbooks.Add(); Worksheets sheets = book.Worksheets.get_Worksheets(); Worksheet sheet = sheets.AddWorksheet(); sheet = sheets[2]; sheet.Cells[1, 1].Value2 = app.InchesToPoints2(1.3).ToString(); sheet.PageSetup.LeftMargin = app.InchesToPoints2(1.3); sheet.Cells[2, 2].FormulaR1C1 = "4,1"; sheet.get_Range("A1", "D3").Font.Size = 20; sheet.get_Range("A1", "D3").Font.Name = "宋体"; sheet.get_Range("A1", "D3").Font.Underline = ReflectOffice.Excel.XlUnderlineStyle.xlUnderlineStyleSingle; sheet.get_Range("A1", "D3").Font.FontStyle = "加粗 倾斜"; sheet.get_Range("A1", "D3").ColumnWidth = 19; sheet.get_Range("A1", "D3").RowHeight = 15; sheet.get_Range("A1", "D3").Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous; sheet.get_Range("A1", "D3").Borders[XlBordersIndex.xlEdgeBottom].Weight = ReflectOffice.Excel.XlBorderWeight.xlThick; sheet.get_Range("A1", "D3").HorizontalAlignment = ReflectOffice.Excel.XlHAlign.xlHAlignCenter; sheet.get_Range("A1", "D3").VerticalAlignment = ReflectOffice.Excel.XlVAlign.xlVAlignCenter; sheet.get_Range("A1", "D3").Rows.AutoFit(); sheet.get_Range("A1", "D3").Columns.AutoFit(); book.SaveAs(@"r:\abc.xls", XlFileFormat.xlExcel8); book.Close(); app.Release(); //Microsoft.Office.Interop.Excel.XlUnderlineStyle; }
private void TempAddTableData(WorksheetPart wsPart, StringValue sheetName, XlSharedStringsTable stringTable) { Worksheet worksheet = wsPart.Worksheet; SheetProperties properties = worksheet.SheetProperties; XlColor color = new XlColor(properties.TabColor.Rgb); XlWorksheet ws = Worksheets.AddWorksheet(sheetName, color, null); List <SheetData> data = new List <SheetData>(worksheet.Elements <SheetData>()); foreach (SheetData sheetData in data) { AddTableData(sheetData, ws, stringTable); } }
private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet) { if (dSpreadsheet.CustomFilePropertiesPart != null) { foreach (var m in dSpreadsheet.CustomFilePropertiesPart.Properties.Elements <CustomDocumentProperty>()) { String name = m.Name.Value; if (m.VTLPWSTR != null) { Properties.AddCustomProperty(name, m.VTLPWSTR.Text); } } } WorkbookPart workbookPart = dSpreadsheet.WorkbookPart; XlSharedStringsTable stringTable = new XlSharedStringsTable(workbookPart, null); var sheets = dSpreadsheet.WorkbookPart.Workbook.Sheets; foreach (Sheet dSheet in sheets.OfType <Sheet>()) { var sheetName = dSheet.Name; var wsPart = dSpreadsheet.WorkbookPart.GetPartById(dSheet.Id) as WorksheetPart; if (wsPart != null) { XlWorksheet ws = Worksheets.AddWorksheet(sheetName); using (var reader = OpenXmlReader.Create(wsPart)) { // We ignore lots of parts. Type[] ignoredElements = new Type[] { typeof(SheetFormatProperties), typeof(SheetViews), typeof(MergeCells), typeof(AutoFilter), typeof(SheetProtection), typeof(DataValidations), typeof(ConditionalFormatting), typeof(Hyperlinks), typeof(PrintOptions), typeof(PageMargins), typeof(PageSetup), typeof(HeaderFooter), typeof(RowBreaks), typeof(Columns), typeof(ColumnBreaks), typeof(LegacyDrawing), typeof(CustomSheetViews) // Custom sheet views contain its own auto filter data, and more, which should be ignored for now }; while (reader.Read()) { while (ignoredElements.Contains(reader.ElementType)) { reader.ReadNextSibling(); } if (reader.ElementType == typeof(Row)) { AddRow(ws, stringTable, (Row)reader.LoadCurrentElement()); } else if (reader.ElementType == typeof(SheetProperties)) { AddSheetTabColor(ws, (SheetProperties)reader.LoadCurrentElement()); } } reader.Close(); } } } }