当前位置:软件学习 > Excel >>

C#中datatabel导出excel(三种方法)

 

方法一:(拷贝直接可以使用,适合大批量资料, 上万笔)

Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application();

SaveFileDialog savefiledialog = new SaveFileDialog();

System.Reflection.Missing miss = System.Reflection.Missing.Value;

appexcel = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbook workbookdata;

Microsoft.Office.Interop.Excel.Worksheet worksheetdata;

Microsoft.Office.Interop.Excel.Range rangedata;

//设置对象不可见

appexcel.Visible = false;

System.Globalization.CultureInfo currentci = System.Threading.Thread.CurrentThread.CurrentCulture;

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");

workbookdata = appexcel.Workbooks.Add(miss);

worksheetdata = (Microsoft.Office.Interop.Excel.Worksheet)workbookdata.Worksheets.Add(miss, miss, miss, miss);

//给工作表赋名称

worksheetdata.Name = "saved";

for (int i = 0; i < dt.Columns.Count; i++)

{

    worksheetdata.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();

}

//因为第一行已经写了表头,所以所有数据都应该从a2开始

rangedata = worksheetdata.get_Range("a2", miss);

Microsoft.Office.Interop.Excel.Range xlrang = null;

//irowcount为实际行数,最大行

int irowcount = dt.Rows.Count;

int iparstedrow = 0, icurrsize = 0;

//ieachsize为每次写行的数值,可以自己设置

int ieachsize = 1000;

//icolumnaccount为实际列数,最大列数

int icolumnaccount = dt.Columns.Count;

//在内存中声明一个ieachsize×icolumnaccount的数组,ieachsize是每次最大存储的行数,icolumnaccount就是存储的实际列数

object[,] objval = new object[ieachsize, icolumnaccount];

icurrsize = ieachsize;

 

 

while (iparstedrow < irowcount)

{

    if ((irowcount - iparstedrow) < ieachsize)

        icurrsize = irowcount - iparstedrow;

    //用for循环给数组赋值

    for (int i = 0; i < icurrsize; i++)

    {

        for (int j = 0; j < icolumnaccount; j++)

            objval[i, j] = dt.Rows[i + iparstedrow][j].ToString();

        System.Windows.Forms.Application.DoEvents();

    }

    string X = "A" + ((int)(iparstedrow + 2)).ToString();

    string col = "";

    if (icolumnaccount <= 26)

    {

        col = ((char)('A' + icolumnaccount - 1)).ToString() + ((int)(iparstedrow + icurrsize + 1)).ToString();

    }

    else

    {

        col = ((char)('A' + (icolumnaccount / 26 - 1))).ToString() + ((char)('A' + (icolumnaccount % 26 - 1))).ToString() + ((int)(iparstedrow + icurrsize + 1)).ToString();

    }

    xlrang = worksheetdata.get_Range(X, col);

    // 调用range的value2属性,把内存中的值赋给excel

    xlrang.Value2 = objval;

    iparstedrow = iparstedrow + icurrsize;

}

//保存工作表

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlrang);

xlrang = null;

//调用方法关闭excel进程

appexcel.Visible = true;

 

 

方法二:(自己建函数,适合大批量资料, 上万笔)

using System.IO;

private void dataTableToCsv(DataTable table, string file)

{

    string title = "";

    FileStream fs = new FileStream(file, FileMode.OpenOrCreate);

    //FileStream fs1 = File.Open(file, FileMode.Open, FileAccess.Read);

    StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);

    for (int i = 0; i < table.Columns.Count; i++)

    {

        title += table.Columns[i].ColumnName + "\t"; //栏位:自动跳到下一单元格

    }

    title = title.Substring(0, title.Length - 1) + "\n";

    sw.Write(title);

    foreach (DataRow row in table.Rows)

    {

        string line = "";

        for (int i = 0; i < table.Columns.Count; i++)

        {

            line += row[i].ToString().Trim() + "\t"; //内容:自动跳到下一单元格

        }

        line = line.Substring(0, line.Length - 1) + "\n";

        sw.Write(line);

    }

    sw.Close();

    fs.Close();

}

dataTableToCsv(dt, @"c:\1.xls"); //调用函数

System.Diagnostics.Process.Start(@"c:\1.xls");  //打开excel文件

 

www.zzzyk.com

方法三:(可以自己调整单元格的格式,适合小批量的数量)

try

{

    //没有数据的话就不往下执行

    if (dataGridView1.Rows.Count == 0)

        return;

    //实例化一个Excel.Application对象

    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

    //让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写

    //excel.Visible = false;

    excel.Visible = true;

    //新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错

    excel.Application.Workbooks.Add(true);

    //生成Excel中列头名称

    for (int i = 0; i < dataGridView1.Columns.Count; i++)

    {

        excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;

    }

    //把DataGridView当前页的数据保存在Excel中

    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)

    {

        for (int j = 0; j < dataGridView1.Columns.Count; j++)

        {

            if (dataGridView1[j, i].ValueType == typeof(string))

            {

                excel.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();

            }

      

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,