Ejemplo n.º 1
0
        private void Open_Command(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == true)
            {
                xmlTest = new GridClass(openFileDialog.FileName);
                gridForXml.DataContext = xmlTest; //обновляем источник данных
            }
        }
Ejemplo n.º 2
0
 public MainWindow()
 {
     xmlTest = new GridClass("XmlTest.xml");
     InitializeComponent();
     gridForXml.DataContext = xmlTest; //источник данных
 }
Ejemplo n.º 3
0
        public static void Export(GridClass xmlTest)
        {
            Microsoft.Office.Interop.Excel.Application oExcel = null;
            Microsoft.Office.Interop.Excel.Workbook    oBook;
            Microsoft.Office.Interop.Excel.Worksheet   oSheet;

            try
            {
                string exportPath = System.AppDomain.CurrentDomain.BaseDirectory;
                exportPath += "ExcelXML";
                exportPath += ".xlsx";

                try
                {
                    oExcel = new Microsoft.Office.Interop.Excel.Application();
                    oBook  = oExcel.Workbooks.Add();
                    oSheet = oExcel.Worksheets[1];
                    podgotovWord(oExcel, false, false);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Файл открыт и используется. Закройте файл и повторите попытку", "Svera Export Word", MessageBoxButton.OK, MessageBoxImage.Information);
                    ExitWord(oExcel, false);
                    return;
                }

                try
                {
                    oBook.SaveAs(exportPath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Файл " + exportPath + " уже открыт. Закройте файл. Повторите попытку", "Svera Export Word", MessageBoxButton.OK, MessageBoxImage.Information);
                    ExitWord(oExcel, false);
                    return;
                }

                string[,] OtableStr = xmlTest.GetSourceToString();

                oSheet.Columns[1].ColumnWidth = 5;
                oSheet.Columns[2].ColumnWidth = 20;
                oSheet.Columns[3].ColumnWidth = 50;
                oSheet.Columns[4].ColumnWidth = 10;
                oSheet.Columns[5].ColumnWidth = 5;



                if (xmlTest.Source != null && xmlTest.Source.Count > 0)
                {
                    Microsoft.Office.Interop.Excel.Range rangeEx = oSheet.Range[oSheet.Cells[1, 1], oSheet.Cells[OtableStr.GetUpperBound(0) + 1, OtableStr.GetUpperBound(1) + 1]];
                    rangeEx.Value     = OtableStr;
                    rangeEx.Font.Name = "Times New Roman";
                    rangeEx.Font.Size = 10;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle         = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle          = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle       = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle        = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].LineStyle   = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.WrapText = true;

                    var scr = xmlTest.DisplayAll.ToList();
                    for (int i = 0; i <= scr.Count - 1; i++)
                    {
                        if (scr[i].Cells.Count == 1)
                        {
                            oSheet.Range[oSheet.Cells[i + 1, 1], oSheet.Cells[i + 1, OtableStr.GetUpperBound(1) + 1]].Merge();
                        }
                    }
                }



                oSheet.Rows.AutoFit();


                oBook.Save();
                ExitWord(oExcel, false);
                oExcel = null;
            }
            catch (Exception ex)
            {
                ExitWord(oExcel, false);

                MessageBox.Show(ex.Message);
                //throw new Exception("WordExport", ex);
            }
        }