.Net常用技巧_傳入DataGrid直接匯出成Excel

來源:互聯網
上載者:User

標籤:datagridview   style   blog   color   os   檔案   

註:非調用OFFICE的DLL方法。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using Utility;using System.IO;namespace MyTool{    public class SaveAsExcel    {        /// <summary>        /// 存為EXCEL(參數為傳過來的DataGridView表格)        /// </summary>        /// <param name="dgv"></param>        public static void DataGridViewToExcel(DataGridView dgv)        {            SaveFileDialog dlg = new SaveFileDialog();            dlg.Filter = "Execl files (*.xls)|*.xls";            dlg.FilterIndex = 0;            dlg.RestoreDirectory = true;            dlg.CreatePrompt = true;            dlg.Title = "儲存為Excel檔案";            if (dlg.ShowDialog() == DialogResult.OK)            {                Stream myStream;                myStream = dlg.OpenFile();                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));                string columnTitle = "";                try                {                    //寫入欄位標題                    for (int i = 0; i < dgv.ColumnCount; i++)                    {                        if (i > 0)                        {                            columnTitle += "\t";                        }                        columnTitle += dgv.Columns[i].HeaderText;                    }                    sw.WriteLine(columnTitle);                    //寫入列內容                    for (int j = 0; j < dgv.Rows.Count; j++)                    {                        string columnValue = "";                        for (int k = 0; k < dgv.Columns.Count; k++)                        {                            if (k > 0)                            {                                columnValue += "\t";                            }                            if (dgv.Rows[j].Cells[k].Value == null)                                columnValue += "";                            else                                columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();                        }                        sw.WriteLine(columnValue);                    }                    sw.Close();                    myStream.Close();                }                catch (Exception e)                {                    MessageBox.Show(e.ToString());                }                finally                {                    sw.Close();                    myStream.Close();                }            }        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.