//見出しを読み込む //シート名は「名簿作成システム」 public override List <DataItems> ReadHeader() { if (Items != null) { return(Items); } if (!File.Exists(FileName)) { return(null); } SpreadsheetDocument Doc = null; try { Doc = SpreadsheetDocument.Open(FileName, false); } catch (Exception) { throw new Exception("データファイル「" + FileName + "」を開けませんでした"); } try { WorkbookPart BPrt = Doc.WorkbookPart; //シート「名簿作成システム」を探す Sheet MeiboSheet = BPrt.Workbook.Descendants <Sheet>().Where(s => s.Name == "名簿作成システム").FirstOrDefault(); if (MeiboSheet == null) { return(null); //見つからない場合はnull } WorksheetPart SPrt = (WorksheetPart)(BPrt.GetPartById(MeiboSheet.Id)); Row FirstRow = SPrt.Worksheet.Descendants <Row>().First(); if (FirstRow == null) { return(null); } Items = new List <DataItems>(); var stringTable = BPrt.GetPartsOfType <SharedStringTablePart>().FirstOrDefault(); for (int i = 0; i < 3; i++) { Cell cl = FirstRow.Descendants <Cell>().Where(c => c.CellReference == (char)((int)'A' + i) + "1").FirstOrDefault(); if (cl == null) { if (Items.Count == 0) { throw new Exception("見出しがおかしいです"); } break; //途中で切れてる } string OneLabel; //データ型によって処理を振り分け switch (cl.DataType.Value) { case CellValues.String: OneLabel = cl.InnerText; break; case CellValues.SharedString: if (stringTable != null) { OneLabel = stringTable.SharedStringTable. ElementAt(int.Parse(cl.InnerText)).InnerText; } else { if (Items.Count == 0) { throw new Exception("見出しのデータがおかしいです"); } goto breakloop; //データ型がおかしい } break; default: if (Items.Count == 0) { throw new Exception("見出しのデータ型がおかしいです"); } goto breakloop; //データ型がおかしい } if (!LabelToEnum.ContainsKey(OneLabel)) { //変な見出しがあれば打ち切り if (Items.Count == 0) { throw new Exception("見出しがおかしいです"); } break; } DataItems HDer = LabelToEnum[OneLabel]; if (Items.Count > 0 && HDer == Items[Items.Count - 1]) { //見出しの順番が正しくない Items.Clear(); throw new Exception("同じ見出しを含んでいます"); } Items.Add(HDer); } breakloop: if (Items.Count == 3 && Items[0] == Items[2]) { Items.Clear(); throw new Exception("同じ見出しを含んでいます"); } } finally { if (Doc != null) { Doc.Close(); } if (Items != null && Items.Count == 0) { Items = null; } } return(Items); }
//見出しを読み込む(try内部から呼び出し) public override List <DataItems> ReadHeader() { if (Items == null) { FileStream fst = null; StreamReader srd = null; try { if (!File.Exists(FileName)) { return(null); //新規作成 } string[] Labels; try { OpenFile(ref fst, ref srd); Labels = srd.ReadLine().Split(new char[1] { '\t' }, 3); //最初の行を読み込んでタブ文字で分割 } catch (Exception) { throw new Exception("データファイル「" + FileName + "」を開く、もしくはデータを読み込むことができませんでした"); } Items = new List <DataItems>(); foreach (string Label in Labels) //Label: 1マスの文字列(見出しが来るはず) { if (Items.Count >= 3) { break; //項目は3つまで } if (!LabelToEnum.ContainsKey(Label)) { //変な見出しがあれば打ち切り if (Items.Count == 0) { throw new Exception("見出しがおかしいです"); } break; } DataItems HDer = LabelToEnum[Label]; if (Items.Count > 0 && HDer == Items[Items.Count - 1]) { //見出しがおかしい Items.Clear(); throw new Exception("同じ見出しを含んでいます"); } Items.Add(HDer); } if (Items.Count == 3 && Items[0] == Items[2]) { Items.Clear(); throw new Exception("同じ見出しを含んでいます"); } } finally { if (fst != null) { CloseFile(ref fst, ref srd); } if (Items != null && Items.Count == 0) { Items = null; } } } return(Items); }