标签: openxml

Excel不插入前导零

我使用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

任何帮助,将不胜感激.

c# openxml spreadsheetml openxml-sdk

2
推荐指数
1
解决办法
7705
查看次数

Word OpenXML替换令牌文本

我正在使用OpenXML来修改Word模板,这些模板包含可由某些字符识别的简单令牌(目前是双V形(ascii 171和187)).

我想用我的文本替换这些标记,这可能是多行的(即从数据库中).

c# ms-word openxml openxml-sdk

2
推荐指数
1
解决办法
4060
查看次数

必须在非泛型静态类中定义扩展方法 - 从文档中提取XML

我目前正在编写一个上传系统,它从文档中获取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)

.net c# xml openxml

2
推荐指数
1
解决办法
3616
查看次数

使用Open Xml将列添加到现有Excel 2007工作簿中

我有一个预定义的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)

.net c# openxml openxml-sdk

2
推荐指数
1
解决办法
6068
查看次数

Openxml单元格返回InnerText而不是实际值

我正在开发一个可重用的代码来读取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)

excel openxml openxml-sdk

2
推荐指数
1
解决办法
2619
查看次数

使用OpenXmlWriter SAX创建带有样式标记的Excel文件

我正在尝试使用OpenXmlWriter(SAX)编写带有样式的Excel Xlsx电子表格.

我能够创建包含行和列的文件(将它们填充为字符串).我正在寻找一个简单的代码,如何使用粗体字形成第一行(标题).

我没有开始的模板文件,因为文件是动态的.

我找到了一些关于如何添加WorkbookStylesPart的文章,但他们都在使用DOM.由于我需要编写大量的行,因此DOM不适用于我.

谁能指出我正确的方向?使用WriteStartElement和OpenXmlAttribute时,将标题行添加为粗体的简单代码.

谢谢,odansky

c# excel openxml

2
推荐指数
1
解决办法
6363
查看次数

ClosedXML生成格式错误的xlsx

当我打开我正在获取的文件时,无法使用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)

excel openxml malformed closedxml

2
推荐指数
1
解决办法
1424
查看次数

如何使用Open XML .net sdk将幻灯片编号添加到powerpoint演示文稿?

我正在尝试使用C#和开放XML将幻灯片编号添加到我的演示文稿中.我已经检查了文档,并在我的演示文稿中添加了幻灯片编号形状,但是当我使用PowerPoint打开演示文稿时,数字不会出现在文本框中,只是说"幻灯片编号".

我希望文本框中填充幻灯片编号,并在我更改PowerPoint演示文稿中的幻灯片位置时动态更新.我怎样才能做到这一点?

c# powerpoint openxml

2
推荐指数
1
解决办法
890
查看次数

使用OpenXML从HTML文件生成docx文件

我正在使用这种方法来生成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 …

c# docx openxml sql-server-openxml

2
推荐指数
2
解决办法
6210
查看次数

使用OpenXML从Excel中删除公式

我正在尝试使用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)

c# openxml openxml-sdk

2
推荐指数
1
解决办法
1141
查看次数