| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2225 人关注过本帖
标题:c#在DataGridView中如何实现查询的内容导出为Excel
只看楼主 加入收藏
lukebc
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:74
专家分:32
注 册:2009-8-18
结帖率:71.43%
收藏
 问题点数:0 回复次数:2 
c#在DataGridView中如何实现查询的内容导出为Excel
c#在DataGridView中如何实现查询出的内容导出为Excel,并可打印
搜索更多相关主题的帖子: Excel 如何 
2016-09-05 09:48
zzqqrr
Rank: 6Rank: 6
来 自:299294817
等 级:侠之大者
威 望:1
帖 子:155
专家分:408
注 册:2011-6-11
收藏
得分:0 
这个比较简单。但代码不少。扣我:2992948171

59izk,com神奇百货,神奇百货网官网,shenqi,一个神奇的导购站!!
2016-09-09 21:56
zzqqrr
Rank: 6Rank: 6
来 自:299294817
等 级:侠之大者
威 望:1
帖 子:155
专家分:408
注 册:2011-6-11
收藏
得分:0 
  public static void Print_DataGridView(DataGridView dgv1)
        {
            try
            {
                try
                {
                    string fileName;
                    ExcelWriter writer;
                    dgv = dgv1;
                    AvailableColumns.Clear();
                    foreach (DataGridViewColumn column in dgv.Columns)
                    {
                        if (column.Visible)
                        {
                            AvailableColumns.Add(column.HeaderText);
                        }
                    }
                    PrintOptions options = new PrintOptions(AvailableColumns);
                    if (options.ShowDialog() == DialogResult.OK)
                    {
                        PrintTitle = options.PrintTitle;
                        PrintAllRows = options.PrintAllRows;
                        FitToPageWidth = options.FitToPageWidth;
                        SelectedColumns = options.GetSelectedColumns();
                        if (!options.EX_excel)
                        {
                            goto Label_0397;
                        }
                        fileName = @"c:\test.xls";
                        SaveFileDialog dialog2 = new SaveFileDialog
                        {
                            Filter = "Excel文件(*.xls)|*.xls",
                            Title = "保存Excel文档"
                        };
                        if (dialog2.ShowDialog() == DialogResult.OK)
                        {
                            fileName = dialog2.FileName;
                            goto Label_012C;
                        }
                    }
                    return;
                Label_012C:
                    writer = new ExcelWriter(fileName);
                    writer.BeginWrite();
                    int num = 0;
                    foreach (DataGridViewColumn column2 in dgv.Columns)
                    {
                        if (column2.Visible && SelectedColumns.Contains(column2.HeaderText))
                        {
                            short y = short.Parse(num.ToString());
                            writer.WriteString(0, y, column2.HeaderText);
                            num++;
                        }
                    }
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        if (!dgv.Rows[i].IsNewRow && (PrintAllRows || dgv.Rows[i].Selected))
                        {
                            short x = short.Parse((i + 1).ToString());
                            int num6 = 0;
                            foreach (DataGridViewColumn column2 in dgv.Columns)
                            {
                                if (column2.Visible && SelectedColumns.Contains(column2.HeaderText))
                                {
                                    short num7 = short.Parse(num6.ToString());
                                    string str2 = "";
                                    if (dgv.Rows[i].Cells[column2.Name].Value != null)
                                    {
                                        str2 = dgv.Rows[i].Cells[column2.Name].Value.ToString().Trim();
                                    }
                                    writer.WriteString(x, num7, str2);
                                    num6++;
                                }
                            }
                        }
                    }
                    writer.EndWrite();
                    MessageBox.Show("保存成功");
                    return;
                Label_0397:
                    if (options.Print_isname && (options.Print_name != ""))
                    {
                        printDoc.PrinterSettings.PrinterName = options.Print_name;
                    }
                    RowsPerPage = 0;
                    PrintPreviewDialog dialog = new PrintPreviewDialog
                    {
                        Document = printDoc
                    };
                    printDoc.BeginPrint += new PrintEventHandler(PrintDGV.PrintDoc_BeginPrint);
                    printDoc.PrintPage += new PrintPageEventHandler(PrintDGV.PrintDoc_PrintPage);
                    if (dialog.ShowDialog() != DialogResult.OK)
                    {
                        printDoc.BeginPrint -= new PrintEventHandler(PrintDGV.PrintDoc_BeginPrint);
                        printDoc.PrintPage -= new PrintPageEventHandler(PrintDGV.PrintDoc_PrintPage);
                    }
                    else
                    {
                        printDoc.Print();
                        printDoc.BeginPrint -= new PrintEventHandler(PrintDGV.PrintDoc_BeginPrint);
                        printDoc.PrintPage -= new PrintPageEventHandler(PrintDGV.PrintDoc_PrintPage);
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "错误!!!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            finally
            {
            }
        }

        private static void PrintDoc_BeginPrint(object sender, PrintEventArgs e)
        {
            try
            {
                StrFormat = new StringFormat();
                StrFormat.Alignment = StringAlignment.Near;
                StrFormat.LineAlignment = StringAlignment.Center;
                StrFormat.Trimming = StringTrimming.EllipsisCharacter;
                StrFormatComboBox = new StringFormat();
                StrFormatComboBox.LineAlignment = StringAlignment.Center;
                StrFormatComboBox.FormatFlags = StringFormatFlags.NoWrap;
                StrFormatComboBox.Trimming = StringTrimming.EllipsisCharacter;
                ColumnLefts.Clear();
                ColumnWidths.Clear();
                ColumnTypes.Clear();
                CellHeight = 0;
                RowsPerPage = 0;
                CellButton = new Button();
                CellCheckBox = new CheckBox();
                CellComboBox = new ComboBox();
                TotalWidth = 0;
                foreach (DataGridViewColumn column in dgv.Columns)
                {
                    if (column.Visible && SelectedColumns.Contains(column.HeaderText))
                    {
                        TotalWidth += column.Width;
                    }
                }
                PageNo = 1;
                NewPage = true;
                RowPos = 0;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

59izk,com神奇百货,神奇百货网官网,shenqi,一个神奇的导购站!!
2016-09-09 22:00
快速回复:c#在DataGridView中如何实现查询的内容导出为Excel
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015535 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved