E-B*_*Bat 2 .net c# openxml openxml-sdk
我有一个预定义的Excel工作簿,其中所有工作表都就位,我需要为其编写内容。我成功地写了单元格。
问题是在一个特定的工作表中,我需要向其中添加三列。在下面的代码中,首先我抓住Worksheet,然后继续添加列。这段代码运行良好,我的意思是,没有引发异常,但是,当我尝试打开Excel文件时,我收到一条错误消息,指出有些内容无法读取,并且此特定工作表的所有内容都已清除。
我知道问题在于此操作,因为如果我注释掉那些添加列的行,则工作簿将随我从代码中写入的所有单元格值一起打开。
这是相关的代码,出于测试目的,我尝试添加3列:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(outputPath, true)){
Sheet sheet2 = document.WorkbookPart.Workbook.Descendants<Sheet>().Single( s => s.Name == "Miscellaneous Credit" );
Worksheet workSheet2 = ( (WorksheetPart)document.WorkbookPart.GetPartById( sheet2.Id ) ).Worksheet;
Columns cs = new Columns();
for ( var y = 1; y <= 3; y++ ) {
Column c = new Column()
{
Min = (UInt32Value)1U,
Max = (UInt32Value)1U,
Width = 44.33203125D,
CustomWidth = true
};
cs.Append( c );
}
workSheet2.Append( cs );
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据克里斯对列的概念的解释
using (SpreadsheetDocument document = SpreadsheetDocument.Open(outputPath, true)){
Sheet sheet2 = document.WorkbookPart.Workbook.Descendants<Sheet>().Single( s => s.Name == "Miscellaneous Credit" );
Worksheet workSheet2 = ( (WorksheetPart)document.WorkbookPart.GetPartById( sheet2.Id ) ).Worksheet;
// Check if the column collection exists
Columns cs = workSheet2.Elements<Columns>().FirstOrDefault();
if ( ( cs == null ) ) {
// If Columns appended to worksheet after sheetdata Excel will throw an error.
SheetData sd = workSheet2.Elements<SheetData>().FirstOrDefault();
if ( ( sd != null ) ) {
cs = workSheet2.InsertBefore( new Columns(), sd );
}
else {
cs = new Columns();
workSheet2.Append( cs );
}
}
//create a column object to define the width of columns 1 to 3
Column c = new Column
{
Min = (UInt32Value)1U,
Max = (UInt32Value)3U,
Width = 44.33203125,
CustomWidth = true
};
cs.Append( c );
}
Run Code Online (Sandbox Code Playgroud)
答案的第一部分讨论如何设置列宽(基于初始示例代码,我认为您只想定义列宽)。
首先,似乎您误解了对象的Min和Max属性Column。它们分别代表受此“列信息”记录影响的第一列和最后一列。因此,如果您有一组具有相同宽度的连续列,则可以使用一个Column类来设置该宽度。在您的代码段中,您定义了同一列(索引1)宽度的3倍。
然后,您认为Columns集合尚不存在...
Columns集合添加到之后SheetData,Excel将抛出error。适用于我的最终代码(Open XML SDK 2.0)
using (SpreadsheetDocument document = SpreadsheetDocument.Open(outputPath, true)) {
Sheet sheet2 = document.WorkbookPart.Workbook.Descendants<Sheet>().Single(s => s.Name == "Your sheet name");
Worksheet workSheet2 = ((WorksheetPart)document.WorkbookPart.GetPartById(sheet2.Id)).Worksheet;
// Check if the column collection exists
Columns cs = workSheet2.Elements<Columns>().FirstOrDefault();
if ((cs == null)) {
// If Columns appended to worksheet after sheetdata Excel will throw an error.
SheetData sd = workSheet2.Elements<SheetData>().FirstOrDefault();
if ((sd != null)) {
cs = workSheet2.InsertBefore(new Columns(), sd);
} else {
cs = new Columns();
workSheet2.Append(cs);
}
}
//create a column object to define the width of columns 1 to 3
Column c = new Column {
Min = (UInt32Value)1U,
Max = (UInt32Value)3U,
Width = 44.33203125,
CustomWidth = true
};
cs.Append(c);
}
Run Code Online (Sandbox Code Playgroud)
我仍然对如何执行列插入感到困惑。说我有A,B和C列,我想在B和C之间插入三列,最后以A,B,C,D,E,F列结尾。我该如何实现?
ColumnsOpenXml SDK中的对象用于存储列的样式和宽度信息。插入Column集合中不会“插入”,在表中的列。
像您所说的那样“插入”列是OpenXmlSDK的非常大而复杂的任务。
从我对问题的理解来看,这意味着您将必须找到所有单元格并通过更改其引用来移动它们(例如,插入3列后,引用为“ B1”的单元格将变为“ F1”等)。这意味着您将不得不更改许多其他内容(例如,公式中单元格的引用)。
使用Office.Interop或使用EEPlus或ClosedXml之类的库可以轻松完成此类任务。
| 归档时间: |
|
| 查看次数: |
6068 次 |
| 最近记录: |