- 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;
- namespace WindowsFormsjj
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- DataSet dss = jobset();
- try
- {
- Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
- myExcel.Application.Workbooks.Add(true);
- myExcel.Visible = true;
- Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)myExcel.Cells[1, 1];
- int iRow = dss.Tables[0].Rows.Count;
- int iCol = dss.Tables[0].Columns.Count;
- for (int j = 0; j < iCol; j++)
- {
- myExcel.Cells[1, j + 1] = dss.Tables[0].Columns[j].ColumnName.ToString();//填充列标题
- }
- for (int i = 0; i < iRow; i++)
- {
- for (int j = 0; j < iCol; j++)
- {
- myExcel.Cells[i + 2, j + 1] = "'" + dss.Tables[0].Rows[i][j].ToString();//填充数据
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- public DataSet jobset()
- {
- SqlConnection con = new SqlConnection("server=.;uid=sa;password=123789;database=pubs");
- SqlDataAdapter da = new SqlDataAdapter("select * from titles", con);
- DataSet ds = new DataSet();
- da.Fill(ds);
- return ds;
- }
- }
- }
【转】DataSet导出EXCEL
最新推荐文章于 2020-09-21 09:13:32 发布