[code=C#][/code] DataTable dt = new DataTable();
dt.Columns.Add("姓名", typeof(string));
dt.Columns.Add("班级", typeof(string));
dt.Columns.Add("年龄", typeof(string));
dt.Columns.Add("身份", typeof(string));
string name1 = tbName.Text;
string banji1 = ddlBanji.SelectedItem.Text;
string sql1 = "and name like '" + name1 + "'or class like'" + banji1 + "'";
table = ada.GetDataBySql(sql1);
//DataRow dr = dt.NewRow();
for (int i = 1; i < table.Rows.Count; i++)
{
DataRow dr = dt.NewRow();
dr["姓名"] = table.Rows[i]["name"].ToString();
dr["班级"] = table.Rows[i]["class"].ToString();
dr["年龄"] = table.Rows[i]["age"].ToString();
dr["身份"] = table.Rows[i]["shenfen"].ToString();
dt.Rows.Add(dr);
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
string ofname = DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
string filename = "C:\\Documents and Settings/Administrator/桌面/" + ofname;
string result = LgwOutFile.exportToExcel(ds, filename);
总是第一行信息不显示
------解决方案--------------------------------------------------------
excel中行的下标是从1开始的,你看下你的导出方法吧
------解决方案--------------------------------------------------------
给个OK的例子给你看看
你慢慢对比下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace Excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//导入命名空间,using Microsoft.Office.Interop.Excel;
//引入组件Microsoft.Office.Interop.Excel;
//打开Excal并且往里面写入东西
private void btn_基本操作_Click(object sender, EventArgs e)
{
//创建一个Excal应用程序
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
//创建工作薄
app.Workbooks.Add();
//创建工作表,为2个
app.SheetsInNewWorkbook = 2;
//获取工作表,下标从1开始
Worksheet w1 = (Worksheet)app.ActiveWorkbook.Worksheets[1];
//设置工作表的名字
w1.Name = "First";
//设置单元格的类容,下标从1开始
w1.Cells[1, 1] = "你好,这里是第一行";
//Worksheet w2 = (Worksheet)app.ActiveWorkbook.Worksheets[2];
////设置工作表的名字
//w2.Name = "Second";
////设置单元格的类容,下标从1开始
//w2.Cells[1, 1] = "ok";
app.Visible = true;//设置可见
//线程睡眠
//System.Threading.Thread.Sleep(2000);//2秒
//保存-------获取exe文件所在的目录,文件名,保存的文件类型
app.ActiveWorkbook.SaveAs(Environment.CurrentDirectory + "/test.xls", XlFileFormat.xlWorkbookNormal);
//关闭当前活动的工作簿
//app.ActiveWorkbook.Close();
//退出应用程序
//app.Quit();
}
//读取数据库,并且导出到Excal
private void btn_读取数据库_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MyCinema;Integrated Security=True");