一个SQL循环,它将为循环中存在的工作表添加唯一记录

时间:2018-06-02 01:40:03

标签: c# sql oracle openxml

当我将一个属引入以下片段时:

...

OracleConnection conn = new OracleConnection();
conn.ConnectionString = orcldb;
conn.Open();

sql = "select genus, species, environment from table_1 where genus = '" + sGenus + "' order by species";

OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();

while (dr.Read()) {
   if (dr["collateral_id"].ToString() != sSpec) {
      IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "CollShell");
      WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.FirstOrDefault().Id);

      cell = GetCell(worksheetPart.Worksheet, "A", 1);
      cell.DataType = new EnumValue<CellValues>(CellValues.String);
      cell.CellValue = new CellValue(dr["genus"].ToString());

      cell = GetCell(worksheetPart.Worksheet, "A", 2);
      cell.DataType = new EnumValue<CellValues>(CellValues.String);
      cell.CellValue = new CellValue(dr["species"].ToString());

      cell = GetCell(worksheetPart.Worksheet, "A", 3);
      cell.DataType = new EnumValue<CellValues>(CellValues.String);
      cell.CellValue = new CellValue(dr["environment"].ToString());

      CopySheet(AppDomain.CurrentDomain.BaseDirectory + "my template file.xlsm", "SpeciesShell", dr["species"].ToString());
      sSpec = dr["species"].ToString();
   }
}   
conn.Close();
conn.dispose();

...

我的模板file.xlsm上名为SpeciesShell的工作表将被复制,工作表副本将被命名为该给定时刻dr.Read循环中的任何物种。

(我提供了CopySheet定义):

static void CopySheet(string filename, string sheetName, string clonedSheetName) {
   using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(filename, true)) {
      WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
      WorksheetPart sourceSheetPart = GetWorkSheetPart(workbookPart, sheetName);

      SpreadsheetDocument tempSheet = SpreadsheetDocument.Create(new MemoryStream(), mySpreadsheet.DocumentType);
      WorkbookPart tempWorkbookPart = tempSheet.AddWorkbookPart();
      WorksheetPart tempWorksheetPart = tempWorkbookPart.AddPart<WorksheetPart>(sourceSheetPart);
      WorksheetPart clonedSheet = workbookPart.AddPart<WorksheetPart>(tempWorksheetPart);
      tempSheet.Close();
      tempSheet.Dispose();

      CleanView(clonedSheet);

      Sheets sheets = workbookPart.Workbook.GetFirstChild<Sheets>();
      Sheet copiedSheet = new Sheet();
      copiedSheet.Name = clonedSheetName;
      copiedSheet.Id = workbookPart.GetIdOfPart(clonedSheet);
      copiedSheet.SheetId = (uint)sheets.ChildElements.Count + 1;
      sheets.Append(copiedSheet);
      workbookPart.Workbook.Save();
   }
}

static void CleanView(WorksheetPart worksheetPart) {
   SheetViews views = worksheetPart.Worksheet.GetFirstChild<SheetViews>();
   if (views != null) {
      views.Remove();
      worksheetPart.Worksheet.Save();
   }
}

到目前为止我刚刚讲述的所有内容都已经奏效。

问题:我对剪辑中最深刻的线条的意图是将独特的物种critera匹配到为该物种命名的表格中。在实践中,这不是发生的事情 - 相反,来自前一个属的最后一个物种被保留并嵌入到所有后续属的文件物种表中。

0 个答案:

没有答案