エクセルファイルがあります。ファイルの最初の 2 列を取得し、これらのセルの値を使用して、クラスの最初の 2 つの文字列メンバーを埋めています。このクラスの残りの文字列メンバーは、クラス コンストラクターで開始および設定されます。このクラスは (ColumnsColumn) と呼ばれます。ColumnsColumn の配列であるメンバーを持つ 2 番目のクラス (Columns と呼ばれる) があります。このクラスは、メソッド XMLSerializer.Selerialize(Stream s, Object o) で「object o」パラメータとして渡しています。このメソッドは、XML をコマンド ラインに出力します。問題は、ColumnsColumn の最初の 2 つのメンバー (Excel ファイルから取得した 2 つ) のみを印刷していることです。
ここに私のProgram.cs(メイン)ファイルがあります:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Xml.Serialization;
namespace Excel_Dictionary
{
public class Program
{
public static Columns c = new Columns();
public static List<ColumnsColumn> objects = new List<ColumnsColumn>();
public static void Main(string[] args)
{
#if (DEBUG)
Parser excel = new Parser("stuff.xlsx");
#else
Console.Write("File name: ");
Parser excel = new Parser(Console.ReadLine());
#endif
XmlSerializer x = new XmlSerializer(typeof(Columns));
List<ColumnsColumn> temp = new List<ColumnsColumn>();
temp = excel.Parse();
c.Items = temp.ToArray();
x.Serialize(Console.Out, c);
Console.ReadLine();
for (int i = 0; i < objects.Count; i++)
{
}
}
}
public class Parser
{
private Excel.Application xlApp;
private Excel.Workbook xlBook;
private Excel.Worksheet xlSheet;
public Parser(string filename)
{
xlApp = new Excel.Application();
xlBook = xlApp.Workbooks.Open(filename);
xlSheet = xlBook.Worksheets[1];
xlApp.Visible = true;
Start();
}
public List<ColumnsColumn> Parse()
{
List<ColumnsColumn> arr = new List<ColumnsColumn>();
Start();
do
{
ColumnsColumn t = new ColumnsColumn();
t.ColumnType = CellValue();
Move(0, 1);
t.FieldKey = CellValue();
Move(1, -1);
t.EmptyValue = "";
t.FieldParameters = "";
t.TypeKey = "";
t.RelationshipIdentifier = "";
t.Title = "";
t.Section = "";
t.ColumnStyle = "";
t.ShowAggregation = "None";
t.AggregationTotalLevel = "All";
t.WeightedByFieldKey = "";
t.WeightedByFieldParameters = "";
t.CalculateProportion = "False";
t.ProportionTo = "GrandTotal";
t.TotalColumnStyle = "";
t.GroupColumnStyle = "";
t.Show = "True";
arr.Add(t);
} while (CellValue() != "" && CellValue() != null);
return arr;
}
public void Move(int row, int col)
{
xlApp.ActiveCell.get_Offset(row, col).Select();
}
/// <returns>Value of Active Cell</returns>
public string CellValue()
{
return xlApp.ActiveCell.Value2;
}
public void Start()
{
xlSheet.get_Range("A2").Select();
}
}
}
XML デシリアライズされたクラスを含む XMLSchema ファイル (サイズに惑わされないでください。多くの get set コマンドがあります):
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.5472
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Columns {
private ColumnsColumn[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Column", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ColumnsColumn[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ColumnsColumn {
private string columnTypeField;
private string fieldKeyField;
private string emptyValueField;
private string fieldParametersField;
private string typeKeyField;
private string relationshipIdentifierField;
private string titleField;
private string sectionField;
private string columnStyleField;
private string showAggregationField;
private string aggregationTotalLevelField;
private string weightedByFieldKeyField;
private string weightedByFieldParametersField;
private string calculateProportionField;
private string proportionToField;
private string totalColumnStyleField;
private string groupColumnStyleField;
private string showField;
public ColumnsColumn() {
this.emptyValueField = "";
this.fieldParametersField = "";
this.typeKeyField = "";
this.relationshipIdentifierField = "";
this.titleField = "";
this.sectionField = "";
this.columnStyleField = "";
this.showAggregationField = "None";
this.aggregationTotalLevelField = "All";
this.weightedByFieldKeyField = "";
this.weightedByFieldParametersField = "";
this.calculateProportionField = "False";
this.proportionToField = "GrandTotal";
this.totalColumnStyleField = "";
this.groupColumnStyleField = "";
this.showField = "True";
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ColumnType {
get {
return this.columnTypeField;
}
set {
this.columnTypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string FieldKey {
get {
return this.fieldKeyField;
}
set {
this.fieldKeyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string EmptyValue {
get {
return this.emptyValueField;
}
set {
this.emptyValueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string FieldParameters {
get {
return this.fieldParametersField;
}
set {
this.fieldParametersField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string TypeKey {
get {
return this.typeKeyField;
}
set {
this.typeKeyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string RelationshipIdentifier {
get {
return this.relationshipIdentifierField;
}
set {
this.relationshipIdentifierField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string Title {
get {
return this.titleField;
}
set {
this.titleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string Section {
get {
return this.sectionField;
}
set {
this.sectionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string ColumnStyle {
get {
return this.columnStyleField;
}
set {
this.columnStyleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("None")]
public string ShowAggregation {
get {
return this.showAggregationField;
}
set {
this.showAggregationField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("All")]
public string AggregationTotalLevel {
get {
return this.aggregationTotalLevelField;
}
set {
this.aggregationTotalLevelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string WeightedByFieldKey {
get {
return this.weightedByFieldKeyField;
}
set {
this.weightedByFieldKeyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string WeightedByFieldParameters {
get {
return this.weightedByFieldParametersField;
}
set {
this.weightedByFieldParametersField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("False")]
public string CalculateProportion {
get {
return this.calculateProportionField;
}
set {
this.calculateProportionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("GrandTotal")]
public string ProportionTo {
get {
return this.proportionToField;
}
set {
this.proportionToField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string TotalColumnStyle {
get {
return this.totalColumnStyleField;
}
set {
this.totalColumnStyleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("")]
public string GroupColumnStyle {
get {
return this.groupColumnStyleField;
}
set {
this.groupColumnStyleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.ComponentModel.DefaultValueAttribute("True")]
public string Show {
get {
return this.showField;
}
set {
this.showField = value;
}
}
}