其他分享
首页 > 其他分享> > OpenXml开发-向Excel2007文档中添加数据

OpenXml开发-向Excel2007文档中添加数据

作者:互联网

原文链接:http://www.cnblogs.com/hjzhang/archive/2009/12/04/2043572.html

public static void Run()
        {
             // 打开Excel2007模板文档
            using (PackageHelper package = new PackageHelper(Properties.Resources.BaseSpreadsheetML))
            {
                // 加载sheet1内容到一个XmlDocument
                Uri sheet1Uri =
                    new Uri(@"/xl/worksheets/sheet1.xml", UriKind.Relative);
                XmlDocument sheet1Xml =
                    package.GetWritablePart(sheet1Uri);
                XPathNavigator sheet1Data = sheet1Xml.CreateNavigator()
                    .SelectSingleNode("x:worksheet/x:sheetData",
                        Namespaces.NamespaceManager);
                //向sheet1添加数据
                using (XmlWriter writer = sheet1Data.AppendChild())
                {
                   int i;
                   //添加标题行
                   Spreadsheet.SpreadsheetDoc.WriteStartRow(writer, 1);
                   Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "A", 1, "Name");
                   Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "B", 1, "Email");
                   Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "C", 1, "Territory");
                   Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "D", 1, "Sub Total");
                   Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "E", 1, "Tax Amount");
                   Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "F", 1, "Freight");
                   Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "G", 1, "Sale Total");
                   writer.WriteEndElement();//关闭标题行标记
                   int index = 2;//从第二行开始填充数据
                   for (i = 0; i < 12; i++)
                   {
                       // 添加行开始标记
                       SpreadsheetDoc.WriteStartRow(writer, index);
                       // 向单元格添加数据
                       Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "A", index,
                           "Name:"+i.ToString());
                       Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "B", index,
                           i.ToString()+"@metarace.com");
                       Spreadsheet.SpreadsheetDoc.WriteStringCell(writer, "C", index,
                           i.ToString());
                       Spreadsheet.SpreadsheetDoc.WriteNumberCell(writer, "D", index,
                           i);
                       Spreadsheet.SpreadsheetDoc.WriteNumberCell(writer, "E", index,
                           i);
                       Spreadsheet.SpreadsheetDoc.WriteNumberCell(writer, "F", index,
                           i);
                       // write the total formula in the cell
                       Spreadsheet.SpreadsheetDoc.WriteFormulaCell(writer, "G", index,
                           string.Format("SUM(D{0}:F{0})", index));
                       // 添加行结束标记
                       writer.WriteEndElement();
                       index++;
                   }
                   // 添加合计
                   SpreadsheetDoc.WriteStartRow(writer, index);
                   // 添加合计单元格的说明和公式
                   SpreadsheetDoc.WriteStringCell(writer, "F", index, "Sales Totals");
                   //添加公式单元格
                   SpreadsheetDoc.WriteFormulaCell(writer, "G", index,
                       string.Format("SUM(G2:G{0})", index - 1));
                   // 关闭行
                   writer.WriteEndElement();
                }              
                //保存
                package.SavePart(sheet1Uri, sheet1Xml);
                // save the shared strings
                SharedStrings.Save(package);
                // 保存到文件
                package.Save("test.xlsx");
            }
        }
       以上代码中的部分方法将在下篇加以说明

转载于:https://www.cnblogs.com/hjzhang/archive/2009/12/04/2043572.html

标签:index,WriteStringCell,SpreadsheetDoc,writer,Excel2007,Spreadsheet,添加,文档,OpenXml
来源: https://blog.csdn.net/weixin_30268071/article/details/98052897