日期:2014-05-18  浏览次数:21005 次

c# 将sql表内容导出到Excel
我做了一个winform现在希望通过点击按钮把sql里指定表(手动指定要如何设计?)的数据导出到Excel。
还有一个是点击把datagridview里面的数据导出到excel怎么写,还请大家指点一下~

------解决方案--------------------
C# code

        private void SaveAs(DataGridView dgvAgeWeekSex)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl 97-2003 (*.xls)|*.xls|Execl 2007 (*.xlsx)|*.xlsx";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Netbor Export Dialog";
            saveFileDialog.ShowDialog();
            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            string str = "";
            try
            {
                for (int i = 0; i < dgvAgeWeekSex.ColumnCount; i++)
                {
                    if (i > 0)
                    {
                        str += "\t";
                    }
                    str += dgvAgeWeekSex.Columns[i].HeaderText;
                }
                sw.WriteLine(str);
                for (int j = 0; j < dgvAgeWeekSex.Rows.Count; j++)
                {
                    string tempStr = "";
                    for (int k = 0; k < dgvAgeWeekSex.Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            tempStr += "\t";
                        }
                        tempStr += dgvAgeWeekSex.Rows[j].Cells[k].Value.ToString();
                    }
                    sw.WriteLine(tempStr);
                }
                sw.Close();
                myStream.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                sw.Close();
                myStream.Close();
            }
        }

------解决方案--------------------
用oledb连接你的excel(需要事先创建一个模板,即带有标题列)
把sql server的数据读到datatable或者reader里,循环结果集,拼insert sql语句来写入xls文件.
最后把连接关掉
dgv也一样,如果数据源是dataset或者datatable就如上炮制,要么就循环dgv的rows,然后还是取每个cell的值来拼sql
------解决方案--------------------
private void button1_Click(object sender, EventArgs e)
{
string pathName = System.Windows.Forms.Application.StartupPath.Trim();
string dateTime = txtdatatime.Text.Split(char.Parse("-"))[0] + "年" + txtdatatime.Text.Split(char.Parse("-"))[1] + "月";

int count = 0;
FileInfo mode = new FileInfo(pathName + "\\model\\Name.xls");
try
{
mode.CopyTo(pathName + "\\temp\\" + dateTime + " 产品名称.xls", true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
object ming = Type.Missing;
Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
myExcel.Application.Workbooks.Open(pathName + "\\temp\\" + dateTime + " 产品名称.xls", ming, ming, ming, ming, ming, ming, ming, ming, ming, ming, ming, ming, ming, ming);
myExcel.Visible = false;

Excel.Workbook myBook = myExcel.Workbooks[1];
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];

databind();

try
{
if (ds.Tables.Contains("tblClass"))//如果有这个表就把改表的内容清空