SpreadSheetDocument 의 LineChart 에 새 데 이 터 를 추가 합 니 다.
코드 는 다음 과 같 습 니 다:
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().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().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<<<<<<<<<<<<().FirstOrDefault();
C.PlotArea pa = c.PlotArea;
C.LineChart lc = pa.Descendants()
.FirstOrDefault();
C.LineChartSeries lcs = new C.LineChartSeries();
C.Index i = new C.Index() { Val = (UInt32Value)3U };
C.Order o = new C.Order() { Val = (UInt32Value)3U };
C.Marker m = new C.Marker();
C.Symbol sy = new C.Symbol()
{
Val = C.MarkerStyleValues.None
};
m.Append(sy);
C.CategoryAxisData cad = new C.CategoryAxisData();
C.StringReference stringReference1 =
new C.StringReference();
C.Formula formula1 = new C.Formula();
formula1.Text = "Sheet1!$A$1:$D$1";
C.StringCache stringCache1 = new C.StringCache();
C.PointCount pointCount1 = new C.PointCount()
{
Val = (UInt32Value)4U
};
C.StringPoint stringPoint1 = new C.StringPoint()
{
Index = (UInt32Value)0U
};
C.PointCount p = new C.PointCount()
{
Val = (UInt32Value)4U
};
stringCache1.Append(p);
C.NumericValue numericValue1 = new C.NumericValue();
numericValue1.Text = "A";
stringPoint1.Append(numericValue1);
C.StringPoint stringPoint2 = new C.StringPoint()
{
Index = (UInt32Value)1U
};
C.NumericValue numericValue2 = new C.NumericValue();
numericValue2.Text = "B";
stringPoint2.Append(numericValue2);
C.StringPoint stringPoint3 = new C.StringPoint()
{
Index = (UInt32Value)2U
};
C.NumericValue numericValue3 = new C.NumericValue();
numericValue3.Text = "C";
stringPoint3.Append(numericValue3);
C.StringPoint stringPoint4 = new C.StringPoint()
{
Index = (UInt32Value)3U
};
C.NumericValue numericValue4 = new C.NumericValue();
numericValue4.Text = "D";
stringPoint4.Append(numericValue4);
stringReference1.Append(formula1);
stringCache1.Append(stringPoint1);
stringCache1.Append(stringPoint2);
stringCache1.Append(stringPoint3);
stringCache1.Append(stringPoint4);
stringReference1.Append(stringCache1);
cad.Append(stringReference1);
C.Values values1 = new Values();
C.NumberReference numberReference1 =
new C.NumberReference();
C.Formula formula2 = new C.Formula();
formula2.Text = "Sheet1!$A$5:$D$5";
C.NumberingCache numberingCache1 =
new C.NumberingCache();
C.FormatCode formatCode1 = new C.FormatCode();
formatCode1.Text = "General";
C.PointCount pointCount2 = new C.PointCount()
{
Val = (UInt32Value)4U
};
C.NumericPoint numericPoint1 = new C.NumericPoint()
{
Index = (UInt32Value)0U
};
C.NumericValue numericValue5 = new C.NumericValue();
numericValue5.Text = "2";
numericPoint1.Append(numericValue5);
C.NumericPoint numericPoint2 = new C.NumericPoint()
{
Index = (UInt32Value)1U
};
C.NumericValue numericValue6 = new C.NumericValue();
numericValue6.Text = "4";
numericPoint2.Append(numericValue6);
C.NumericPoint numericPoint3 = new C.NumericPoint()
{
Index = (UInt32Value)2U
};
C.NumericValue numericValue7 = new C.NumericValue();
numericValue7.Text = "3";
numericPoint3.Append(numericValue7);
C.NumericPoint numericPoint4 = new C.NumericPoint()
{
Index = (UInt32Value)3U
};
C.NumericValue numericValue8 = new C.NumericValue();
numericValue8.Text = "2";
numericPoint4.Append(numericValue8);
numberingCache1.Append(formatCode1);
numberingCache1.Append(pointCount1);
numberingCache1.Append(numericPoint1);
numberingCache1.Append(numericPoint2);
numberingCache1.Append(numericPoint3);
numberingCache1.Append(numericPoint4);
numberReference1.Append(formula2);
numberReference1.Append(numberingCache1);
values1.Append(numberReference1);
C.Smooth smth = new C.Smooth() { Val = false };
lcs.Append(i);
lcs.Append(o);
lcs.Append(m);
lcs.Append(cad);
lcs.Append(values1);
lcs.Append(smth);
C.LineChartSeries lst = lc
.Descendants().Last();
lc.InsertAfter(lcs, lst);
w.Save();
}
else
{
log.Fatal("Can't get ChartPart");
Console.ReadKey();
return;
}
}
else
{
log.Fatal("Can't get DrawingsPart");
Console.ReadKey();
return;
}
}
else
{
log.Fatal("Can't get WorksheetPart");
}
Console.ReadKey();
}
}
static Cell AddCell(string reference, string value)
{
Cell result = new Cell() { CellReference = reference };
CellValue cv = new CellValue() { Text = value };
result.Append(cv);
return result;
}
}
}
효 과 는 다음 과 같 습 니 다:
《 허 양의 붉 은 진흙 집 》 에 오신 것 을 환영 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Office 365 License 자동 할당텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.