我使用Office OpenXml写入Excel文件.该文件是一个模板,因此它已经包含了我的列的所有标题和格式.我将具有前导零的数字插入到"特殊"列中,该列基本上是10位数字.但是在我的代码中,我可以看到它正在设置值,例如0000000004.工作表中的结果,该4单元格中的值和实际单元格显示0000000004.
这是我写入单元格的代码.
if (reader[2].ToString().Length < 9)
{
myworksheet.Cell(firstrow, 12).Value = reader[2].ToString(); //0045678945
}
else
{
myworksheet.Cell(firstrow, 12).Value = reader[2].ToString().Substring(0, 9); //0045678945
}
Run Code Online (Sandbox Code Playgroud)
当我像上面所述打开excel表时,我的价值45678945不是0045678945
任何帮助,将不胜感激.
我正在使用OpenXML来修改Word模板,这些模板包含可由某些字符识别的简单令牌(目前是双V形(ascii 171和187)).
我想用我的文本替换这些标记,这可能是多行的(即从数据库中).
我目前正在编写一个上传系统,它从文档中获取XML并将其显示在网页上.
我面临的问题是,每当我添加网站的XML提取部分时,编译器将返回标题中提到的错误.
我的代码目前看起来像这样.
导致问题的部分是publicstatic Xnamespace w以及以下所有相关的XML代码.干杯.
<script runat="server">
//This template is being built to allow the viewstates to be displayed and allow them to be hidden
//or shown at the same time. The buttons are being added so we can test whether they will
//be hidden on page use
public string _TempFileLocation = ""; //Used to locate Word Document File Path
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string viewStateDisplay1 = "ViewState1 is now being displayed";
string …Run Code Online (Sandbox Code Playgroud) 我有一个预定义的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 = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个可重用的代码来读取excel单元格值.我的代码如下:
private string ReadCellValue(WorksheetPart worksheetPart, string cellAddress)
{
string value = null;
Cell theCell = worksheetPart.Worksheet.Descendants<Cell>().
Where(c => c.CellReference == cellAddress).FirstOrDefault();
// If the cell does not exist, return an empty string.
if (theCell != null)
{
value = theCell.InnerText;
if (theCell.DataType != null)
{
switch (theCell.DataType.Value)
{
case CellValues.SharedString:
var stringTable =
worksheetPart.GetPartsOfType<SharedStringTablePart>()
.FirstOrDefault();
if (stringTable != null)
{
value =
stringTable.SharedStringTable
.ElementAt(int.Parse(value)).InnerText;
}
break;
case CellValues.Boolean:
switch (value)
{
case "0":
value = "FALSE";
break;
default:
value = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用OpenXmlWriter(SAX)编写带有样式的Excel Xlsx电子表格.
我能够创建包含行和列的文件(将它们填充为字符串).我正在寻找一个简单的代码,如何使用粗体字形成第一行(标题).
我没有开始的模板文件,因为文件是动态的.
我找到了一些关于如何添加WorkbookStylesPart的文章,但他们都在使用DOM.由于我需要编写大量的行,因此DOM不适用于我.
谁能指出我正确的方向?使用WriteStartElement和OpenXmlAttribute时,将标题行添加为粗体的简单代码.
谢谢,odansky
当我打开我正在获取的文件时,无法使用ClosedXML输出一个好的文件
我们发现'filename.xlsx'中的某些内容存在问题.您是否希望我们尽可能多地恢复?如果您信任此工作簿的来源,请单击"是".
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.AddWorksheet("name");
worksheet.Row(1).Cell(i + 1).SetValue("test");
worksheet.Row(k + 2).Cell(column.Order + 1).SetValue("test value");
using (var memoryStream = new MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream.GetBuffer();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用C#和开放XML将幻灯片编号添加到我的演示文稿中.我已经检查了文档,并在我的演示文稿中添加了幻灯片编号形状,但是当我使用PowerPoint打开演示文稿时,数字不会出现在文本框中,只是说"幻灯片编号".
我希望文本框中填充幻灯片编号,并在我更改PowerPoint演示文稿中的幻灯片位置时动态更新.我怎样才能做到这一点?
我正在使用这种方法来生成docx文件:
public static void CreateDocument(string documentFileName, string text)
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Create(documentFileName, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
string docXml =
@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body><w:p><w:r><w:t>#REPLACE#</w:t></w:r></w:p></w:body>
</w:document>";
docXml = docXml.Replace("#REPLACE#", text);
using (Stream stream = mainPart.GetStream())
{
byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
stream.Write(buf, 0, buf.Length);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力:
CreateDocument("test.docx", "Hello");
Run Code Online (Sandbox Code Playgroud)
但是,如果我想放置HTML内容而不是Hello?例如:
CreateDocument("test.docx", @"<html><head></head>
<body>
<h1>Hello</h1>
</body>
</html>");
Run Code Online (Sandbox Code Playgroud)
或类似这样的东西:
CreateDocument("test.docx", @"Hello<BR>
This is a simple text<BR>
Third paragraph<BR>
Sign
");
Run Code Online (Sandbox Code Playgroud)
两种情况都为创建了无效的结构document.xml …
我正在尝试使用openxml从工作表中删除所有公式。这是我正在尝试的:
internal static void ReplaceFormulaWithValue()
{
var res = _worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
foreach (Row row in _worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>())
{
foreach (Cell cell in row.Elements<Cell>())
{
if (cell.CellFormula != null &&
cell.CellValue != null)
{
string cellRef = cell.CellReference;
CalculationChainPart calculationChainPart = _spreadSheet.WorkbookPart.CalculationChainPart;
CalculationChain calculationChain = calculationChainPart.CalculationChain;
var calculationCells = calculationChain.Elements<CalculationCell>().ToList();
CalculationCell calculationCell = calculationCells.Where(c => c.CellReference == cellRef).FirstOrDefault();
//CalculationCell calculationCell = calculationChain.Elements<CalculationCell>().Where(c => c.CellReference == cell.CellReference).FirstOrDefault();
string value = cell.CellValue.InnerText;
UpdateCell(cell, DataTypes.String, value);
cell.CellFormula.Remove();
calculationCell.Remove();
}
}
}
SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
打开excel文档时,出现以下错误:
<?xml version="1.0" …Run Code Online (Sandbox Code Playgroud)