Exemplo n.º 1
0
        public void Convert(Excel.Worksheet worksheet, IDelimitedTextWriter textWriter)
        {
            using (Excel.Range usedRange = worksheet.UsedRange)
            {
                Int32 rowCount;
                using (Excel.Range rows = usedRange.Rows)
                    rowCount = rows.Count;


                Int32 columnCount;
                using (Excel.Range columns = usedRange.Columns)
                    columnCount = columns.Count;

                for (Int32 rowIndex = 1; rowIndex <= rowCount; rowIndex += 1)
                {
                    Int32 rightColumn = 0;

                    for (Int32 columnIndex = 1; columnIndex <= columnCount; columnIndex += 1)
                    {
                        if (rowIndex > 1 && columnIndex == 1)
                        {
                            textWriter.EndRecord();
                        }
                        else if (columnIndex > 1)
                        {
                            textWriter.EndField();
                        }

                        using (Excel.Range cellRange = (Excel.Range)usedRange.Cells[rowIndex, columnIndex])
                        {
                            String text = (String)cellRange.Text;
                            textWriter.Write(text);

                            if (String.IsNullOrEmpty(text) == false)
                            {
                                rightColumn = columnIndex;
                            }
                        }
                    }

                    this.OnRowConverted(rowIndex, rowCount);
                }
            }
        }
Exemplo n.º 2
0
        public static void Convert(String xlsxFilePath, IDelimitedTextWriter textWriter, WorksheetConverter worksheetConverter = null)
        {
            if (xlsxFilePath == null)
            {
                throw new ArgumentNullException(nameof(xlsxFilePath));
            }
            if (textWriter == null)
            {
                throw new ArgumentNullException(nameof(textWriter));
            }

            if (System.IO.File.Exists(xlsxFilePath) == false)
            {
                throw new FileNotFoundException(String.Format("\"{0}\" file has not been found.", xlsxFilePath));
            }

            if (worksheetConverter == null)
            {
                worksheetConverter = new WorksheetConverter();
            }

            using (Excel.Application excelApplication = new Excel.Application())
            {
                try
                {
                    excelApplication.Visible        = false;
                    excelApplication.ScreenUpdating = false;
                    excelApplication.EnableEvents   = false;

                    using (Excel.Workbooks workbooks = excelApplication.Workbooks)
                    {
                        using (Excel.Workbook emptyExcelWorkbook =
                                   workbooks.Count == 0 ? workbooks.Add() : null)
                        {
                            try
                            {
                                excelApplication.Calculation = XlCalculation.xlCalculationManual;

                                using (var excelWorkbook = workbooks.Open(filename: Path.GetFullPath(xlsxFilePath)))
                                {
                                    Object activeSheet = excelWorkbook.ActiveSheet;
                                    try
                                    {
                                        if (activeSheet is Excel.Worksheet == false)
                                        {
                                            throw new ActiveWorksheetException();
                                        }

                                        Excel.Worksheet worksheet = (Excel.Worksheet)activeSheet;
                                        worksheetConverter.Convert(worksheet, textWriter);
                                    }
                                    finally
                                    {
                                        (activeSheet as IDisposable)?.Dispose();
                                    }
                                }
                            }
                            finally
                            {
                                emptyExcelWorkbook?.Close(saveChanges: false);
                            }
                        }
                    }
                }
                finally
                {
                    excelApplication.Quit();
                }
            }
        }