示例#1
0
        private void ExportButton_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
            Workbook  ExcelWorkBook;
            Worksheet ExcelWorkSheet;
            Range     ExcelRange;
            object    misValue = System.Reflection.Missing.Value;

            ExcelWorkBook  = ExcelApp.Workbooks.Add(misValue);
            ExcelWorkSheet = (Worksheet)ExcelWorkBook.Worksheets.get_Item(1);
            ExcelWorkSheet.Columns.HorizontalAlignment = 3;
            ExcelWorkSheet.Cells[1, 1]  = "Магазин";
            ExcelWorkSheet.Cells[1, 2]  = "Артикул";
            ExcelWorkSheet.Cells[1, 3]  = "Наименование";
            ExcelWorkSheet.Cells[1, 4]  = "1 нед. назад";
            ExcelWorkSheet.Cells[1, 5]  = "2 нед. назад";
            ExcelWorkSheet.Cells[1, 6]  = "3 нед. назад";
            ExcelWorkSheet.Cells[1, 7]  = "Прошл. ср. продажи";
            ExcelWorkSheet.Cells[1, 8]  = "1 нед. вперед";
            ExcelWorkSheet.Cells[1, 9]  = "2 нед. вперед";
            ExcelWorkSheet.Cells[1, 10] = "3 нед. вперед";
            ExcelWorkSheet.Cells[1, 11] = "Буд. ср. продажи";
            ExcelWorkSheet.Cells[1, 12] = "Текущие продажи";
            ExcelWorkSheet.Cells[1, 13] = "Потр. магазина";
            ExcelWorkSheet.Cells[1, 14] = "Наличие";
            ExcelWorkSheet.Cells[1, 15] = "Поставки";
            ExcelWorkSheet.Cells[1, 16] = "Транзит";
            ExcelWorkSheet.Cells[1, 17] = "Будет";
            ExcelWorkSheet.Cells[1, 18] = "Минимум";
            ExcelWorkSheet.Cells[1, 19] = "Максимум ср.";
            ExcelWorkSheet.Cells[1, 20] = "Максимум";
            ExcelWorkSheet.Cells[1, 21] = "Прогноз";
            ExcelWorkSheet.Cells[1, 22] = "Дельта";
            ExcelWorkSheet.Cells[1, 23] = "SL";
            ExcelWorkSheet.Cells[1, 24] = "Остаток на СГП";
            ExcelWorkSheet.Cells[1, 25] = "ПЗ";
            ExcelWorkSheet.Cells[1, 26] = "Будущие ПЗ";
            ExcelWorkSheet.Cells[1, 27] = "Начало анализа";
            ExcelWorkSheet.Cells[1, 28] = "Конец анализа";
            object[,] objData           = new object[PlanDataGrid.Rows.Count, PlanDataGrid.Columns.Count];
            for (int j = 0; j < PlanDataGrid.Columns.Count; j++)
            {
                for (int i = 0; i < PlanDataGrid.Rows.Count; i++)
                {
                    objData[i, j] = (j == 1 ? "'" : "") + PlanDataGrid.Rows[i].Cells[j].Value.ToString();
                }
            }
            ExcelRange           = ExcelWorkSheet.get_Range("A1", misValue);
            ExcelRange           = ExcelRange.get_Resize(1, PlanDataGrid.Columns.Count);
            ExcelRange.Font.Bold = true;
            ExcelRange.AutoFilter(1, ExcelRange, XlAutoFilterOperator.xlAnd, misValue, true);
            ExcelRange                   = ExcelWorkSheet.get_Range("A2", misValue);
            ExcelRange                   = ExcelRange.get_Resize(PlanDataGrid.Rows.Count, PlanDataGrid.Columns.Count);
            ExcelRange.Value2            = objData;
            ExcelRange                   = ExcelWorkSheet.get_Range("A1", misValue);
            ExcelRange                   = ExcelRange.get_Resize(PlanDataGrid.Rows.Count + 1, PlanDataGrid.Columns.Count);
            ExcelRange.Borders.LineStyle = 1;
            ExcelRange.Columns.AutoFit();
            ExcelRange.Rows.AutoFit();
            ExcelApp.Interactive    = true;
            ExcelApp.ScreenUpdating = true;
            ExcelApp.Visible        = true;
            ExcelApp.UserControl    = true;
        }