Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using DevExpress.Spreadsheet;
- using System.IO;
- using DevExpress.Data.Filtering;
- using DevExpress.Xpo;
- using DevExpress.ExpressApp;
- using DevExpress.ExpressApp.Win;
- using System.Threading;
- using System.Globalization;
- namespace kashiash.utils
- {
- public interface IImportujeExcela
- {
- void WczytajWiersz(int i, ExcelImporterHelper importHelper);
- IObjectSpace ObjectSpace { set; }
- }
- public abstract class ExcelImporter : IImportujeExcela
- {
- public IObjectSpace objectSpace = null;
- public IObjectSpace ObjectSpace
- {
- set
- {
- objectSpace = value;
- }
- }
- public abstract void WczytajWiersz(int i, ExcelImporterHelper importHelper);
- }
- public class ExcellImporter : IDisposable
- {
- Workbook workbook = null;
- Worksheet worksheet = null;
- IObjectSpace objectSpace = null;
- IImportujeExcela importer = null;
- ExcelImporterHelper importHelper = null;
- string IdentifyString = string.Empty;
- int startRow = 0;
- WinApplication winApplication = null;
- private readonly string[,] splashUpdates = new string[,]{
- { "Prosze czekać...", "Rozpoczynamy..." },
- { "A bit more...", "Wczytuję dane..." },
- { "Trwa zapisywanie do bazy!", "Kończymy..." } };
- public ExcellImporter(IObjectSpace os)
- {
- objectSpace = os;
- }
- public ExcellImporter(IImportujeExcela ie, int headerRow, string identify)
- {
- IdentifyString = identify;
- startRow = headerRow;
- importer = ie;
- }
- public ExcellImporter(IObjectSpace os, IImportujeExcela ie, string identify)
- {
- objectSpace = os;
- importer = ie;
- IdentifyString = identify;
- }
- public ExcellImporter(IObjectSpace os, IImportujeExcela ie, WinApplication wap, string identify)
- {
- objectSpace = os;
- importer = ie;
- IdentifyString = identify;
- winApplication = wap;
- }
- public ExcellImporter(IObjectSpace os, IImportujeExcela ie, WinApplication wap, int headerRow, string identify)
- {
- objectSpace = os;
- importer = ie;
- IdentifyString = identify;
- startRow = headerRow;
- winApplication = wap;
- }
- public string[] PobierzPliki()
- {
- OpenFileDialog openFileDialog1 = new OpenFileDialog();
- openFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|All files (*.*)|*.*";
- // openFileDialog1.FilterIndex = 1;
- openFileDialog1.RestoreDirectory = true;
- openFileDialog1.Multiselect = true;
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- return openFileDialog1.FileNames;
- }
- return new string[0];
- }
- public void Execute()
- {
- if (winApplication != null)
- {
- // Set the WinApplication.SplashScreen property to provide a splash screen for your application.
- winApplication.SplashScreen = new DevExpress.ExpressApp.Win.Utils.DXSplashScreen();
- // Call the SetDisplayText to provide the initial display text for your splash screen.
- winApplication.SplashScreen.SetDisplayText(splashUpdates[0, 0]);
- }
- string[] pliki = PobierzPliki();
- foreach (string fileName in pliki)
- {
- if (winApplication != null)
- {
- winApplication.StartSplash();
- ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[0, 0], splashUpdates[0, 1]);
- }
- try
- {
- Importuj(fileName);
- }
- finally
- {
- if (winApplication != null)
- {
- ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[2, 0], splashUpdates[2, 1]);
- winApplication.StopSplash();
- }
- }
- }
- if (winApplication != null)
- {
- ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[2, 0], splashUpdates[2, 1]);
- winApplication.StopSplash();
- }
- }
- public void Execute(string fileName)
- {
- try
- {
- Importuj(fileName);
- }
- finally
- {
- }
- }
- private void Importuj(string excelFilePath)
- {
- if (objectSpace != null)
- {
- importer.ObjectSpace = objectSpace;
- }
- if (!File.Exists(excelFilePath)) throw new FileNotFoundException(excelFilePath);
- using (workbook = new Workbook())
- {
- workbook.LoadDocument(excelFilePath);
- worksheet = workbook.Worksheets[0];
- using (importHelper = new ExcelImporterHelper(worksheet))
- {
- Cell Rodzaj = worksheet.Rows[0]["A"];
- if (winApplication != null)
- {
- ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(excelFilePath, splashUpdates[1, 1]);
- }
- if (IdentifyString == string.Empty || Rodzaj.Value.ToString().Trim() == IdentifyString)
- {
- Range usedRange = worksheet.GetUsedRange();
- int lk = usedRange.RowCount;
- int prog = 30;
- for (int i = 1; i < lk; i++)
- {
- if (i >= startRow)
- {
- if (winApplication != null)
- {
- if (i / lk * 100 > prog)
- {
- prog += 10;
- string description = $"{excelFilePath} {prog}%%";
- ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(excelFilePath, description);
- }
- }
- importer.WczytajWiersz(i, importHelper);
- }
- }
- if (winApplication != null)
- {
- ((ISupportUpdateSplash)winApplication.SplashScreen).UpdateSplash(splashUpdates[2, 0], splashUpdates[2, 1]);
- }
- if (objectSpace != null)
- {
- objectSpace.CommitChanges();
- objectSpace.Refresh();
- }
- }
- }
- }
- }
- public void Dispose()
- {
- //
- }
- }
- public class ExcelImporterHelper : IDisposable
- {
- Worksheet worksheet = null;
- public ExcelImporterHelper(Worksheet _worksheet)
- {
- worksheet = _worksheet;
- }
- public DateTime GetCellDateValue(int i, string v)
- {
- Cell dataCell = worksheet.Rows[i][v];
- return kashiash.utils.XUtilConvert.ParsujDate(dataCell.Value.ToString().Trim());
- }
- public decimal GetCellDecimalValue(int i, string v)
- {
- Cell decimalCell = worksheet.Rows[i][v];
- return System.Convert.ToDecimal(decimalCell.Value.NumericValue);
- }
- public decimal GetCellDecimalValue(int i, string v, string culture)
- {
- Cell decimalCell = worksheet.Rows[i][v];
- return decimal.Parse(decimalCell.Value.ToString().Trim(), new CultureInfo(culture));
- }
- public string GetCellStringValue(int i, string v)
- {
- Cell stringCell = worksheet.Rows[i][v];
- return stringCell.Value.ToString().Trim();
- }
- public int GetCellIntValue(int i, string v)
- {
- Cell cell = worksheet.Rows[i][v];
- return kashiash.utils.XUtilConvert.ObjectToInt(cell.Value.ToString().Trim());
- }
- public void Dispose()
- {
- //this.Dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement