当前位置:编程学习 > C#/ASP.NET >>

向SpreadSheetDocument中的LineChart添加新的数据

原始文件如下:

 

代码如下:


[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using log4net; 
using System.Windows.Forms; 
using DocumentFormat.OpenXml.Packaging; 
using System.IO; 
using DocumentFormat.OpenXml.Spreadsheet; 
using DocumentFormat.OpenXml; 
using C = DocumentFormat.OpenXml.Drawing.Charts; 
 
namespace ExcelAppendChartData 

    class Program 
    { 
        private static ILog log = log4net.LogManager.GetLogger(typeof(Program)); 
         
        [STAThread] 
        static void Main(string[] args) 
        { 
            string source = string.Empty; 
            string target = string.Empty; 
            OpenFileDialog ofd = new OpenFileDialog(); 
            ofd.Title = "Open Source Spreadsheet Document"; 
            ofd.Multiselect = false; 
            ofd.Filter = "Spreadsheet Document (*.xlsx)|*.xlsx"; 
            ofd.InitialDirectory = @"C:\Users\v-yangxu\Desktop\Work"; 
            ofd.ShowDialog(); 
            source = ofd.FileName; 
            target = AppDomain.CurrentDomain.BaseDirectory + "Result.xlsx"; 
            File.Copy(source, target, true); 
            using (SpreadsheetDocument sd = SpreadsheetDocument 
                .Open(target, true)) 
            { 
                WorkbookPart wp = sd.WorkbookPart; 
                log.Info("Get WorkbookPart"); 
                Workbook wb = wp.Workbook; 
                log.Info("Get Workbook"); 
                Sheet s = wb.Descendants<Sheet>().Where(S => S.Name == "Sheet1") 
                    .FirstOrDefault(); 
                log.Info("Sheet rId : " + s.Id); 
                WorksheetPart wsp = wp.GetPartById(s.Id) as WorksheetPart; 
                if (wsp != null) 
                { 
                    log.Info("Get WorksheetPart"); 
                    Worksheet w = wsp.Worksheet; 
                    SheetData sda = w.Descendants<SheetData>().FirstOrDefault(); 
                    Row r = new Row() { RowIndex = (UInt32Value)5U }; 
                    log.Info("=========Begin to Append Data=========="); 
                    Cell c1 = AddCell("A5", "2"); 
                    log.Info("A5 : 2"); 
                    Cell c2 = AddCell("B5", "4"); 
                    log.Info("B5 : 4"); 
                    Cell c3 = AddCell("C5", "3"); 
                    log.Info("C5 : 3"); 
                    Cell c4 = AddCell("D5", "2"); 
                    log.Info("D5 : 2"); 
                    r.Append(c1); 
                    r.Append(c2); 
                    r.Append(c3); 
                    r.Append(c4); 
                    sda.Append(r); 
                    log.Info(">>>>>>>>End Append<<<<<<<<<<<<<"); 
                    SheetDimension sdms = w.SheetDimension; 
                    sdms.Reference = "A1:D5"; 
                    DrawingsPart dp = wsp.DrawingsPart; 
                    if (d

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,