本文介绍了如何将数据集导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,


我在按钮单击事件中有以下代码.

sir,


I have following code.in button click event.

            dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Adress", typeof(string));
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "hi";
                dr[1] = "Chennai";
                dt.Rows.Add(dr);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);



using (var wrt = new StreamWriter(openFile.FileName))
{
    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
    {
        wrt.Write(ds.Tables[0].Columns[i].ToString().Trim() + "\t");
    }
    wrt.WriteLine();
    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
    {
        if (ds.Tables[0].Rows[j][0].ToString() != null)
        {
            wrt.Write(ds.Tables[0].Rows[j][0].ToString().Trim());
            for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
            {
                wrt.Write("\t" + ds.Tables[0].Rows[j][i].ToString().ToString().Trim());
            }
            wrt.WriteLine();
        }
    }



当IAM单击按钮IAM直接打开Excel文件.但我想得到

打开保存取消选项.为此我该怎么做

有什么解决办法请帮帮我.

谢谢&问候



When iam click button iam getting directly Opening Excel file.But i Want to get

open save Cancel options. For this what can I do

Is there any solution Please help me.

Thanks & Regards

推荐答案

SaveFileDialog sfileD = new SaveFileDialog();
sfileD.Filter = "xlsx files(*.xlsx)|*.xlsx|xls files(*.xls)|*.xls|All files(*.*)|*.*";
sfileD.FilterIndex = 2;
sfileD.RestoreDirectory = true;
if (sfileD.ShowDialog() == DialogResult.OK)
{
 string filename = sfileD.FileNames[0];
 if (exportToExcel(ds.Tables[0], filename))
  MessageBox.Show("Excel Sheet generated successfully.");
}



方法exportTOExcel



Method exportTOExcel

public static bool exportToExcel(DataTable source, string fileName)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets.Add("Sheet1");
            WorksheetRow row = sheet.Table.Rows.Add();
            WorksheetStyle excelHeaderStyle = book.Styles.Add("HeaderRowStyle");
            excelHeaderStyle.Font.Bold = true;
            excelHeaderStyle.Font.FontName = "Verdana";
            excelHeaderStyle.Font.Size = 10;
            excelHeaderStyle.Font.Color = "#000000";
            try
            {
                for (int i = 0; i < source.Columns.Count; i++)
                {
                    row.Cells.Add(source.Columns[i].ColumnName, DataType.String, "HeaderRowStyle");
                }
                int no_colums = source.Columns.Count;
                for (int i = 0; i < source.Rows.Count; i++)
                {
                    row = sheet.Table.Rows.Add();
                    for (int j = 0; j < no_colums; j++)
                    {
                        row.Cells.Add(source.Rows[i].ItemArray[j].ToString());
                    }
                }
                book.Save(fileName);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }




这篇关于如何将数据集导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:15