private void btnLerArquivo_Click(object sender, EventArgs e) { var filePath = textBox1.Text; if (!File.Exists(filePath)) { MessageHelper.ShowWarning("File does not exist"); return; } if (!Path.GetExtension(filePath).IsInList(".po", ".xlsx")) { MessageHelper.ShowWarning("File extension not recognized"); return; } try { var file = PoSheetReader.Read(filePath); _currentFile = file; dataGridView1.DataSource = file.Records; MessageHelper.ShowInfo("Leitura concluída"); } catch (Exception ex) { MessageHelper.ShowError("Erro ao ler o arquivo: " + ex.Message); } }
public static PoSheet Read(string filePath) { var file = new PoSheet(); WorkBook workbook = WorkBook.Load(filePath); WorkSheet sheet = workbook.WorkSheets.First(); var lines = sheet.Rows; for (int i = PoSheet.StartLine; i < lines.Count; i++) { var line = lines[i]; var currentRecord = new PoRecord() { msgid = line.Columns[0].StringValue, msgstr = line.Columns[1].StringValue }; file.AddRecord(currentRecord); } return(file); }
public static PoSheet Read(string filePath) { var file = new PoSheet(); Application xlApp = new Application(); Workbook xlWorkbook = xlApp.Workbooks.Open(filePath); _Worksheet xlWorksheet = xlWorkbook.Sheets[1]; Range xlRange = xlWorksheet.UsedRange; var lines = xlRange.Rows; for (int i = PoSheet.StartLine; i < lines.Count; i++) { var currentRecord = new PoRecord() { msgid = lines[i, 1].Value, msgstr = lines[i, 2].Value }; file.AddRecord(currentRecord); } return(file); }
public static void Override(string filePath, PoSheet sourceFile) { var linesToRead = File.ReadAllLines(filePath); var linesToWrite = new string[linesToRead.Length]; linesToRead.CopyTo(linesToWrite, 0); PoRecord currentRecord = null; for (int i = PoFile.StartLine; i < linesToRead.Length; i++) { var currentLine = linesToRead[i]; if (currentRecord == null) { if (currentLine.StartsWith(PoFile.KeyIdentifier)) { currentRecord = new PoRecord() { msgid = ExtractTextFromLine(currentLine) }; } } else if (currentLine.StartsWith(PoFile.ValueIdentifier)) { var sourceRecord = sourceFile.Records.FirstOrDefault(x => x.msgid == currentRecord.msgid); if (sourceRecord != null) { linesToWrite[i] = BuildValueLine(sourceRecord.msgstr); } currentRecord = null; } else { currentRecord = null; } } File.WriteAllLines(filePath, linesToWrite); }