Exemple #1
0
        public static string IndWrite(string dataFilename, string filename)
        {
            var checkFiles = new List <string> {
                dataFilename, filename
            }.Select(s => new FileInfo(Path.Combine(Environment.CurrentDirectory, s))).ToList();

            if (checkFiles.Any(info => !info.Exists))
            {
                return($"Could not find file {checkFiles.First(info => !info.Exists)}.");
            }

            try
            {
                IEnumerable <string[]> lines = File.ReadLines(checkFiles[0].FullName, Encoding.UTF8).Select(s => s.Split('\t'));

                ExcelPackage package    = new ExcelPackage(checkFiles[1]);
                int          lineNumber = 1;
                foreach (string[] field in lines)
                {
                    if (field.Length != 2)
                    {
                        Console.Error.Write($"Line #{lineNumber} does not have 2 fields. Found {field.Length} fields.\n");
                        continue;
                    }

                    bool success = XlWriteUtilities.TryParseCellReference(field[0], out Cell cell);
                    if (!success)
                    {
                        Console.Error.Write($"Could not parse cell reference {field[0]}.\n");
                        continue;
                    }

                    ExcelWorksheet sheet = XlWriteUtilities.SheetFromCell(package, cell);

                    sheet.Cells[cell.Row, cell.Column].Value = GetValue(field[1]);

                    lineNumber++;
                }

                package.Save();
            }
            catch (Exception)
            {
                return("There was an error in the writing.");
            }

            return("");
        }
Exemple #2
0
        public static string BlockWrite(string cellReference, string dataFilename, string filename)
        {
            bool success = XlWriteUtilities.TryParseCellReference(cellReference, out Cell startCellLocation);

            if (!success)
            {
                return($"Could not parse the cell reference {cellReference}.");
            }

            List <FileInfo> checkFiles = new List <string> {
                dataFilename
            }
            .Where(name => !string.Equals("-", name))
            .Select(s => new FileInfo(Path.Combine(Environment.CurrentDirectory, s)))
            .ToList();

            if (checkFiles.Any(info => !info.Exists))
            {
                return($"Could not find file {checkFiles.First(info => !info.Exists)}.");
            }

            List <string> li;

            try
            {
                if (dataFilename == "-")
                {
                    li = new List <string>();
                    using TextReader reader = Console.In;
                    string text;
                    while ((text = reader.ReadLine()) != null)
                    {
                        li.Add(text);
                    }
                }
                else
                {
                    FileInfo fullDataFilename = new FileInfo(Path.Combine(Environment.CurrentDirectory, dataFilename));
                    li = File.ReadLines(fullDataFilename.FullName, Encoding.UTF8).ToList();
                }
            }
            catch (Exception)
            {
                return("Could not read data from data source.");
            }


            IEnumerable <string[]>           lines = li.Select(s => s.Split('\t'));
            List <(Cell cell, string value)> cells = new List <(Cell cell, string value)>();

            int index = 0;

            foreach (string[] fields in lines)
            {
                int fieldIndex = 0;
                foreach (string field in fields)
                {
                    cells.Add((new Cell {
                        Row = startCellLocation.Row + index, Column = startCellLocation.Column + fieldIndex
                    }, field));
                    fieldIndex++;
                }
                index++;
            }
            try
            {
                FileInfo     excelFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, filename));
                ExcelPackage package   = new ExcelPackage(excelFile);

                if (!excelFile.Exists)
                {
                    package.Workbook.Worksheets.Add("Sheet 1");
                }

                ExcelWorksheet sheet = XlWriteUtilities.SheetFromCell(package, startCellLocation);

                foreach ((Cell cell, string value) in cells)
                {
                    sheet.Cells[cell.Row, cell.Column].Value = GetValue(value);
                }
                package.Save();
            }
            catch (Exception exception)
            {
                string errorMessage = $"There was an error with writing the data to the excel file.\n{exception.Message}\n";
                if (exception.InnerException != null)
                {
                    errorMessage += exception.InnerException.Message;
                }

                return(errorMessage);
            }

            return("");
        }