如何使用带c#的OpenXMLSDK2.0向Excel.xlsx文件添加新工作表?
只是发布了我今天制定的解决方案。见下面我的答案 如果您没有非常有用的OpenXMLSDK2.0工具,可以在 如果您知道我用“我不知道…”注释的行的用途,请留下解释它们的注释。如何使用带c#的OpenXMLSDK2.0向Excel.xlsx文件添加新工作表?,c#,excel-2007,openxml-sdk,C#,Excel 2007,Openxml Sdk,只是发布了我今天制定的解决方案。见下面我的答案 如果您没有非常有用的OpenXMLSDK2.0工具,可以在 如果您知道我用“我不知道…”注释的行的用途,请留下解释它们的注释。使用(SpreadsheetDocument SpreadsheetDocument=SpreadsheetDocument.Open(spreadSheetFileName,true)){ using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocumen
使用(SpreadsheetDocument SpreadsheetDocument=SpreadsheetDocument.Open(spreadSheetFileName,true)){
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(spreadSheetFileName, true)) {
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
// rId must be unique within the spreadsheet.
// You might be able to use the SpreadSheetDocument.Parts.Count() to do this.
// i.e. string relationshipID = "rId" + (spreadsheetDocument.Parts.Count() + 1).ToString();
string rId = "rId6";
// Sheet.Name and Sheet.SheetId must be unique within the spreadsheet.
Sheet sheet = new Sheet() { Name = "Sheet4", SheetId = 4U, Id = rId };
workbookPart.Workbook.Sheets.Append(sheet);
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(rId);
Worksheet worksheet = new Worksheet();
worksheet.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
// I don't know what SheetDimension.Reference is used for, it doesn't seem to change the resulting xml.
SheetDimension sheetDimension = new SheetDimension() { Reference = "A1:A3" };
SheetViews sheetViews = new SheetViews();
// If more than one SheetView.TabSelected is set to true, it looks like Excel just picks the first one.
SheetView sheetView = new SheetView() { TabSelected = false, WorkbookViewId = 0U };
// I don't know what Selection.ActiveCell is used for, it doesn't seem to change the resulting xml.
Selection selection = new Selection() { ActiveCell = "A1", SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A1" } };
sheetView.Append(selection);
sheetViews.Append(sheetView);
SheetFormatProperties sheetFormatProperties = new SheetFormatProperties() { DefaultRowHeight = 15D };
SheetData sheetData = new SheetData();
// I don't know what the InnerText of Row.Spans is used for. It doesn't seem to change the resulting xml.
Row row = new Row() { RowIndex = 1U, Spans = new ListValue<StringValue>() { InnerText = "1:3" } };
Cell cell1 = new Cell() { CellReference = "A1", DataType = CellValues.Number, CellValue = new CellValue("99") };
Cell cell2 = new Cell() { CellReference = "B1", DataType = CellValues.Number, CellValue = new CellValue("55") };
Cell cell3 = new Cell() { CellReference = "C1", DataType = CellValues.Number, CellValue = new CellValue("33") };
row.Append(cell1);
row.Append(cell2);
row.Append(cell3);
sheetData.Append(row);
PageMargins pageMargins = new PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.7D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D };
worksheet.Append(sheetDimension);
worksheet.Append(sheetViews);
worksheet.Append(sheetFormatProperties);
worksheet.Append(sheetData);
worksheet.Append(pageMargins);
worksheetPart.Worksheet = worksheet;
}
WorkbookPart WorkbookPart=电子表格文档.WorkbookPart;
//rId在电子表格中必须是唯一的。
//您可以使用SpreadSheetDocument.Parts.Count()来完成此操作。
//即字符串relationshipID=“rId”+(spreadsheetDocument.Parts.Count()+1).ToString();
string rId=“rId6”;
//Sheet.Name和Sheet.SheetId在电子表格中必须是唯一的。
Sheet Sheet=new Sheet(){Name=“Sheet4”,SheetId=4U,Id=rId};
workbookPart.Workbook.Sheets.Append(工作表);
WorksheetPart WorksheetPart=workbookPart.AddNewPart(rId);
工作表=新工作表();
工作表.AddNamespaceDeclaration(“r”http://schemas.openxmlformats.org/officeDocument/2006/relationships");
//我不知道SheetDimension.Reference用于什么,它似乎不会改变结果xml。
SheetDimension SheetDimension=新的SheetDimension(){Reference=“A1:A3”};
SheetViews SheetViews=新的SheetViews();
//如果多个SheetView.TabSelected设置为true,则Excel似乎只选择第一个。
SheetView SheetView=new SheetView(){TabSelected=false,WorkbookViewId=0U};
//我不知道Selection.ActiveCell用于什么,它似乎没有改变结果xml。
Selection Selection=newselection(){ActiveCell=“A1”,SequenceOfReferences=newlistvalue(){InnerText=“A1”};
sheetView.Append(选择);
sheetView.Append(sheetView);
SheetFormatProperties SheetFormatProperties=new SheetFormatProperties(){DefaultRowHeight=15D};
SheetData SheetData=新的SheetData();
//我不知道Row.Spans的InnerText用于什么。它似乎没有改变结果xml。
Row Row=new Row(){ROWDINDEX=1U,Spans=new ListValue(){InnerText=“1:3”};
Cell cell1=new Cell(){CellReference=“A1”,数据类型=CellValues.Number,CellValue=new CellValue(“99”)};
Cell cell2=new Cell(){CellReference=“B1”,数据类型=CellValues.Number,CellValue=new CellValue(“55”)};
Cell cell3=new Cell(){CellReference=“C1”,数据类型=CellValues.Number,CellValue=new CellValue(“33”);
行。追加(单元格1);
行。追加(单元格2);
行。追加(第3单元格);
sheetData.Append(行);
PageMargins PageMargins=new PageMargins(){Left=0.7D,Right=0.7D,Top=0.7D,Bottom=0.75D,Header=0.3D,Footer=0.3D};
工作表。追加(表维度);
工作表。附加(图纸视图);
工作表.附加(sheetFormatProperties);
工作表。附加(表数据);
工作表。追加(页边距);
工作表部分工作表=工作表;
}
我不知道Selection.ActiveCell用于什么
Excel打开时,ActiveCell周围有一个焦点矩形。
A1是创建新电子表格时的ActivCell默认值。
可以使用Selection.ActiveCell将ActiveCell设置为任何单元格。我不知道Selection.ActiveCell用于什么
Excel打开时,ActiveCell周围有一个焦点矩形。A1是打开新电子表格时的ActivCell默认值。可以使用Selection.ActiveCell将ActiveCell设置为任何单元格
(2) 我不知道图纸尺寸。参考用于
板材尺寸。参考范围包括“A4:BA25”
A4是第一个有值的单元格,BA25是最后一个。
我不知道Excel是如何使用这些信息的,但是OpenXml并没有为空的行、列和单元格维护xml。SheetDimension.Reference表示A4之前没有值的单元格,BA25之后没有值的单元格