Skip to content

Commit

Permalink
Added the ability to run an Excel file
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinkmrMask committed Mar 15, 2021
1 parent 12b33ab commit 9aef903
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 78 deletions.
5 changes: 4 additions & 1 deletion Program/Information.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
mc:Ignorable="d"
Title="Information" Height="450" Width="800">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<TextBox x:Name="block" IsReadOnly="True"/>
<Grid>
<TextBox x:Name="block" IsReadOnly="True"/>
<Button x:Name="btn" Margin="80,0" Content="Открыть в MS Excel" Background="Azure" Height="52" VerticalAlignment="Bottom" FontFamily="Times New Roman" FontSize="14" Click="btn_Click"/>
</Grid>
</ScrollViewer>
</Window>
153 changes: 93 additions & 60 deletions Program/Information.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
using System.Linq;
using System.Windows;
using System.Xml;
using System;

namespace WpfApp3
{
public partial class Information : Window
{

const string path = "../../Resources/";
const string name = "declarationInfo.xlsx";
double netWeightQuantity;
double grossWeightQuantity;
double positions;
Expand All @@ -34,7 +36,7 @@ public Information(string path)
};
this.awb = new List<(string, string)>();
InitializeComponent();
XmlHandler(path);
xmlHandler(path);
}
finally
{
Expand All @@ -45,33 +47,32 @@ public Information(string path)
}

void close(IWorkbook wb)
{
{
if (!Directory.Exists(path)) Directory.CreateDirectory(path);

const string path = @"D:\Test\test.xlsx";
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
wb.Write(fs);
using (FileStream fs = new FileStream(path + name, FileMode.Create, FileAccess.Write))
wb.Write(fs);
wb.Close();
}
(IWorkbook,ISheet) open()
(IWorkbook, ISheet) open()
{
const string sheetName = "Лист 1";
const string sheetName = "info";
IWorkbook wb = new XSSFWorkbook();
ISheet sh = wb.CreateSheet(sheetName);
return (wb, sh);
}

IWorkbook save(in string[] row, (IWorkbook,ISheet) book)
IWorkbook save(in string[] row, (IWorkbook, ISheet) book)
{
if (row == null)
MessageBox.Show("row is null");
else
if(row != null)
{
print(row);
IRow currentRow = book.Item2.CreateRow(book.Item2.LastRowNum + 1);
const int countColumn = 2;
for (int j = 0; j < countColumn; j++)
for (int j = 0; j < countColumn; j++)
{
currentRow.CreateCell(j).SetCellValue(row[j]);


book.Item2.AutoSizeColumn(j);
}
return book.Item1;
}
return default;
Expand All @@ -94,7 +95,7 @@ void calc(string[] value, ref double result)
void print(string[] value)
{
if (value != null)
block.Text += $"{value[0]} - {value[1]}\n";
block.Text += $"{value[0]} | {value[1]}\n";
}
string[] search(string value, XmlNode collection, string name)
{
Expand All @@ -103,7 +104,7 @@ string[] search(string value, XmlNode collection, string name)
return new string[] { name, element.InnerText };
return default;
}
void XmlHandler(string path)
void xmlHandler(string path)
{
XmlDocument document = new XmlDocument();
document.Load(path);
Expand All @@ -125,69 +126,75 @@ void XmlHandler(string path)
{
if (general.Name == "ESADout_CUGoodsShipment")
{
save(search("catESAD_cu:TotalPackageNumber", general, "Количество мест"), book);
save(search("catESAD_cu:TotalCustCost", general, "Итоговая таможенная стоимоть"), book);
}
foreach (XmlNode info in general.ChildNodes)
{
if (info.Name == "ESADout_CUGoodsLocation")
{
save(search("catESAD_cu:TotalPackageNumber", general, "Количество мест"), book);
save(search("catESAD_cu:TotalCustCost", general, "Итоговая таможенная стоимоть"), book);
save(search("CustomsOffice", info, "Пост прибытия"), book);
}
foreach (XmlNode info in general.ChildNodes)
if (info.Name == "ESADout_CUConsigment")
{
foreach(XmlNode transport in info.ChildNodes)
save(search("catESAD_cu:TransportIdentifier", info, "Транспорт"), book);
}
if (info.Name == "ESADout_CUMainContractTerms")
{
save(search("catESAD_cu:ContractCurrencyCode", info, "Валюта"), book);
save(search("catESAD_cu:ContractCurrencyRate", info, "Курс"), book);
save(search("ESADout_CUMainContractTerms", info, "Итоговая фактурная стоимоть"), book);
}
if (info.Name == "ESADout_CUGoods")
{
if (info.Name == "ESADout_CUGoodsLocation") save(search("CustomsOffice", info, "Пост прибытия"), book);
if (info.Name == "ESADout_CUConsigment") save(search("catESAD_cu:TransportIdentifier", info, "Транспорт"), book);
if (info.Name == "ESADout_CUMainContractTerms")
{
save(search("catESAD_cu:ContractCurrencyCode", info, "Валюта"), book);
save(search("catESAD_cu:ContractCurrencyRate", info, "Курс"), book);
save(search("ESADout_CUMainContractTerms", info, "Итоговая фактурная стоимоть"), book);
calc(search("catESAD_cu:GrossWeightQuantity", info, "Масса брутто"), ref grossWeightQuantity);
calc(search("catESAD_cu:NetWeightQuantity", info, "Масса нетто"), ref netWeightQuantity);
}
if (info.Name == "ESADout_CUGoods")
foreach (XmlNode product in info.ChildNodes)
{
foreach (XmlNode count in product.ChildNodes)
if (count.Name == "catESAD_cu:GoodsGroupInformation")
foreach (XmlNode about in count)
if (about.Name == "catESAD_cu:GoodsGroupQuantity")
calc(search("cat_ru:GoodsQuantity", about, "Количество товара"), ref positions);
}
foreach (XmlNode doc in info)
{
if (doc.Name == "ESADout_CUPresentedDocument")
{
calc(search("catESAD_cu:GrossWeightQuantity", info, "Масса брутто"), ref grossWeightQuantity);
calc(search("catESAD_cu:NetWeightQuantity", info, "Масса нетто"), ref netWeightQuantity);
}
foreach (XmlNode product in info.ChildNodes)
{
foreach (XmlNode count in product.ChildNodes)
if (count.Name == "catESAD_cu:GoodsGroupInformation")
foreach (XmlNode about in count)
if (about.Name == "catESAD_cu:GoodsGroupQuantity")
calc(search("cat_ru:GoodsQuantity", about, "Количество товара"), ref positions);

}
foreach (XmlNode doc in info)
{
if (doc.Name == "ESADout_CUPresentedDocument")
try
{
try
{
if (checkDocumentCode(search("catESAD_cu:PresentedDocumentModeCode", doc, "Классификационный номер документа")))
{
add(search("cat_ru:PrDocumentName", doc, "Документ"));
add(search("cat_ru:PrDocumentNumber", doc, "Номер документа"));
}
}
catch (System.ArgumentException)
if (checkDocumentCode(search("catESAD_cu:PresentedDocumentModeCode", doc, "Классификационный номер документа")))
{
/*
╲╲┏━╮╲╲╲╱╱╱╭━┓╱╱
╲╲┣╮┃╲╲╲╱╱╱┃╭┫╱╱
╲╲┣╯╰┻┻┻┻┻┻╯╰┫╱╱
╲╲┃╱┈┈╲┊┊╱┈┈╲┃╱╱
╲╲┃╰┳┳╯◢◣╰┳┳╯┃╱╱
╲╲╰━━━╰━━╯━━━╯╱╱
*/
add(search("cat_ru:PrDocumentName", doc, "Документ"));
add(search("cat_ru:PrDocumentNumber", doc, "Номер документа"));
}
}
catch (System.ArgumentException)
{
/*
╲╲┏━╮╲╲╲╱╱╱╭━┓╱╱
╲╲┣╮┃╲╲╲╱╱╱┃╭┫╱╱
╲╲┣╯╰┻┻┻┻┻┻╯╰┫╱╱
╲╲┃╱┈┈╲┊┊╱┈┈╲┃╱╱
╲╲┃╰┳┳╯◢◣╰┳┳╯┃╱╱
╲╲╰━━━╰━━╯━━━╯╱╱
*/
}
}
}
}

}
}
}
}
}
}
{

save(new string[] { "Общая масса брутто", grossWeightQuantity.ToString() }, book);
save(new string[] { "Общая масса нетто", netWeightQuantity.ToString() }, book);
save(new string[] { "Всего позиций", positions.ToString() }, book);
Expand All @@ -198,5 +205,31 @@ void XmlHandler(string path)
}
close(book.Item1);
}

private void btn_Click(object sender, RoutedEventArgs e)
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType == null)
{
MessageBox.Show(
"Похоже MS Excel не установлен на Вашем пк,\n Вы можете скопировать содержимое текстового поля\nили прекратить операцию",
"Warning",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
else
{
try
{
Process.Start(Path.GetFullPath(path + name));
}
catch (System.ComponentModel.Win32Exception)
{
MessageBox.Show("Вы прекратили установку MS Excel", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
}
}


}
}
}
10 changes: 0 additions & 10 deletions Program/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Program/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,4 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="Background" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Background.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
4 changes: 0 additions & 4 deletions Program/WpfApp3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<None Include="WpfApp3_TemporaryKey.pfx" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand All @@ -166,8 +165,5 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\Background.jpg" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit 9aef903

Please sign in to comment.