/// <summary> /// create new worksheet from given workbook and name (AddExcelWorksheetToWorkbook node) /// </summary> /// <param name="wbook"></param> /// <param name="sheetName"></param> /// <param name="overWrite"></param> internal WorkSheet(WorkBook wbook, string sheetName, bool overWrite = false) { wb = wbook; // Look for an existing worksheet WorkSheet[] worksheets = wbook.WorkSheets; WorkSheet wSheet = worksheets.FirstOrDefault(n => n.ws.Name == sheetName); if (wSheet == null) { // If you don't find one, create one. ws = (Worksheet)wb.Add(); ws.Name = sheetName; wb.Save(); return; } // If you find one, then use it. if (overWrite) { // if there is only one worksheet, we need to add one more // before we can delete the first one ws = (Worksheet)wb.Add(); wSheet.ws.Delete(); ws.Name = sheetName; wb.Save(); } else { ws = wSheet.ws; } }
public static void EscreveExcel(string pNomeArquivo, ObjetoResultadoComparacao pResultadoComparacao) { //object[][] saida = new object[3][] { new string[2] { "A", "B" }, new string[2] { "C", "D" }, new string[2] { "E", "F" } }; string nomeComExt = pNomeArquivo; Stream k = GetResourceFileStream("Template"); using (var fileStream = File.Create(nomeComExt)) { k.Seek(0, SeekOrigin.Begin); k.CopyTo(fileStream); } WorkBook wb = WorkBook.ReadExcelFile(nomeComExt); WorkSheet abaNcs = wb.GetWorksheetByName("PMV-NCs"); WorkSheet abaMelhorias = wb.GetWorksheetByName("PMV-Melhorias"); //dados = lista de listas onde cada lista é uma linha var dados = abaNcs.Data; //aba.WriteData(0, 0, saida); EscreveDivergenciasExcel(abaNcs, pResultadoComparacao, _primeiraLinhaNCExcel, true); EscreveDivergenciasExcel(abaMelhorias, pResultadoComparacao, _primeiraLinhaMelhoriaExcel, false); wb.Save(); }
public static List <List <string> >[] LerExcelVariasAbas(string pCaminho, string[] pAba) { WorkBook wb = WorkBook.ReadExcelFile(pCaminho); List <List <string> >[] saida = new List <List <string> > [pAba.Length]; for (int i = 0; i < pAba.Length; i++) { WorkSheet ws = wb.GetWorksheetByName(pAba[i]); saida[i] = ws.Data.Select(x => x.Select(k => k == null ? "" : k.ToString()).ToList()).ToList(); } return(saida); }
/// <summary> /// Escreve os resultados contidos no ObjetoResultadoComparacao no arquivo Excel /// </summary> /// <param name="pAbaNcs"></param> /// <param name="pResultado"></param> /// <param name="pIndexPrimeiraLinha"></param> /// <param name="pNCOuMelhoria"></param> public static void EscreveDivergenciasExcel(WorkSheet pAbaNcs, ObjetoResultadoComparacao pResultado, int pIndexPrimeiraLinha, bool pNCOuMelhoria) { List <string[]> listaTextosLinhas = new List <string[]>(); foreach (EstruturaComparacao estrutura in pResultado.ListaEstruturasComparadas) { //string codigoEstrutura = estrutura.NomeEstruturaSaida.Split('-').First(); string codigoEstrutura = estrutura.NomeEstruturaSaida; foreach (PosicaoComparacao posicao in estrutura.ListaPosicoesComErros) { List <ErroPosicao> errosConsiderar = posicao.ListaErrosPosicao.Where(x => x.NcOuMelhoria == pNCOuMelhoria).ToList(); if (errosConsiderar.Count > 0) { List <string> linha = new List <string>(); linha.Add(posicao.NumeracaoString); string descricaoErro = string.Join("; ", errosConsiderar.Select(x => x.EscreveErroExcel())); linha.Add(descricaoErro); linha.Add(""); linha.Add(codigoEstrutura); listaTextosLinhas.Add(linha.ToArray()); } } if (pNCOuMelhoria) { foreach (string posicao in estrutura.PosicoesSomenteListaOriginal.Distinct()) { List <string> linha = new List <string>(); linha.Add(posicao); string descricaoErro = string.Format("Posição {0} eliminada", posicao); linha.Add(descricaoErro); linha.Add(""); linha.Add(codigoEstrutura); listaTextosLinhas.Add(linha.ToArray()); } foreach (string posicao in estrutura.PosicoesSomenteListaNova.Distinct()) { List <string> linha = new List <string>(); linha.Add(posicao); string descricaoErro = string.Format("Criada posição {0}", posicao); linha.Add(descricaoErro); linha.Add(""); linha.Add(codigoEstrutura); listaTextosLinhas.Add(linha.ToArray()); } } } if (pNCOuMelhoria) { foreach (DivergenciaEntreEstruturaLista divergenciaMesmaLista in pResultado.ListaDivergenciasMesmaLista) { List <string> linha = new List <string>(); linha.Add(divergenciaMesmaLista.Posicao); string descricaoErro = string.Format(divergenciaMesmaLista.EscreveErroExcel()); linha.Add(descricaoErro); listaTextosLinhas.Add(linha.ToArray()); } int erro = 1; foreach (string estrutura in pResultado.EstruturasSomenteListaOriginal) { List <string> linha = new List <string>(); linha.Add(string.Format("ENE-{0}", erro)); erro++; string descricaoErro = string.Format("Estrutura {0} não encontrada na lista nova", estrutura); linha.Add(descricaoErro); listaTextosLinhas.Add(linha.ToArray()); } foreach (string estrutura in pResultado.EstruturasSomenteListaNova) { List <string> linha = new List <string>(); linha.Add(string.Format("ENE-{0}", erro)); erro++; string descricaoErro = string.Format("Estrutura {0} não encontrada na lista original", estrutura); linha.Add(descricaoErro); listaTextosLinhas.Add(linha.ToArray()); } pAbaNcs.WriteData(pIndexPrimeiraLinha, 2, listaTextosLinhas.ToArray()); } else { pAbaNcs.WriteData(pIndexPrimeiraLinha, 0, listaTextosLinhas.ToArray()); } }