Ejemplo n.º 1
0
        private void exportGiaoDan1()
        {
            Memory.ClearError();

            provider = new DataProvider(Path.Combine(Memory.AppPath, Memory.DbName), Memory.DbUser, Memory.DbPassword);

            tblGiaoDan = provider.GetData("SELECT * FROM GiaoDan");
            if (Memory.HasError())
            {
                return;
            }
            int count = tblGiaoDan.Rows.Count;

            Memory.Instance.SetMemory("TongDuLieu", count);

            string      fileName = Memory.GetTempPath("GiaoDan.xls");
            ExcelEngine excel    = new ExcelEngine();

            excel.CreateNewObject(fileName);

            for (int i = 0; i < tblGiaoDan.Columns.Count; i++)
            {
                excel.Write_to_excel(1, i + 1, tblGiaoDan.Columns[i].Caption);
            }
            int rowIndex        = 2;
            int currentProgress = 0;

            foreach (DataRow row in tblGiaoDan.Rows)
            {
                currentProgress++;
                Memory.Instance.SetMemory("DuLieuHienTai", currentProgress);

                for (int i = 0; i < tblGiaoDan.Columns.Count; i++)
                {
                    excel.Write_to_excel(rowIndex, i + 1, row[i]);
                }
                rowIndex++;
                if (OnExecuting != null)
                {
                    OnExecuting(string.Concat(((int)(currentProgress * 100 / count)).ToString(), "%"), EventArgs.Empty);
                }
            }
            excel.End_Write();
            System.Diagnostics.Process.Start(fileName);
        }
Ejemplo n.º 2
0
        public static int Export(DataSet ds)
        {
            try
            {
                ExcelEngine excel    = new ExcelEngine();
                string      fileName = "BieuDoSoSanhGiaoHo.xls";

                string outputPath = Memory.GetTempPath(fileName);

                excel.CreateNewObject(outputPath);
                excel.SetGridLines(true);

                DataTable tblData  = ds.Tables[0];
                int       startRow = 28;
                int       col      = 1;
                int       row      = startRow;

                excel.Write_to_excel(row, col, "");
                col++;
                for (int i = 0; i < tblData.Columns.Count; i++)
                {
                    excel.Write_to_excel(row, i + col, "'" + tblData.Columns[i].ColumnName);
                }
                row++;
                col = 1;
                excel.Write_to_excel(row, col, "Số Giáo dân");
                col++;
                for (int i = 0; i < tblData.Rows.Count; i++)
                {
                    for (int j = 0; j < tblData.Columns.Count; j++)
                    {
                        excel.Write_to_excel(row + i, j + col, tblData.Rows[i][j]);
                    }
                }
                string cell1 = "A" + startRow.ToString();
                string cell2 = excel.MapColIndexToColName(tblData.Columns.Count + 1) + (startRow + tblData.Rows.Count).ToString();

                int width  = 600;
                int height = 350;
                if (tblData.Columns.Count > 5)
                {
                    width += 50 * (int)(tblData.Columns.Count - 5);
                }
                if (width > 900)
                {
                    width = 900;
                }
                ChartData chartData = new ChartData();
                chartData.title = "Biểu đồ so sánh giáo họ";
                if (tblData.Columns.Count > 7)
                {
                    chartData.chartType = ChartType.xlColumnClustered;
                }
                else
                {
                    chartData.chartType   = ChartType.xl3DPie;
                    chartData.showPercent = true;
                }
                chartData.cellDataFrom = cell1;
                chartData.cellDataTo   = cell2;
                chartData.width        = width;
                chartData.height       = height;
                chartData.showLegend   = true;
                chartData.showLabel    = true;

                excel.createChart(chartData);
                excel.End_Write();
                System.Diagnostics.Process.Start(outputPath);
            }
            catch (Exception ex)
            {
                Memory.Instance.Error = ex;
                return(-1);
            }
            return(0);
        }