我现在 不知道从SQLSERVER中的表中的数据调到Excel中。显示的效果是什么??是不是还是网页形式???怎么我的老是很怪异???
Import Namespace="System.Data
Import Namespace="System.Data.SqlClient
//定义方法GetData(),返回一个数据表
private System.Data.DataTable GetData()
{
SqlConnection conn= new SqlConnection("server=(local);database=Northwind;uid=sa;pwd=jiao");
SqlDataAdapter adapter= new SqlDataAdapter(("select Id,UserId,Password,Ip,Name from "),conn);
DataSet ds=new DataSet();
try
{
adapter.Fill(ds,"UserId");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0]; }//按钮
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0; excel.Application.Workbooks.Add(true);
DataTable table=GetData();
//将所得到的表的列名,赋值给单元格
foreach(DataColumn col in table1.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
} //同样方法处理数据
foreach(DataRow row in table1.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table1.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//不可见,即后台处理
excel.Visible=true;
}
Import Namespace="System.Data
Import Namespace="System.Data.SqlClient
//定义方法GetData(),返回一个数据表
private System.Data.DataTable GetData()
{
SqlConnection conn= new SqlConnection("server=(local);database=Northwind;uid=sa;pwd=jiao");
SqlDataAdapter adapter= new SqlDataAdapter(("select Id,UserId,Password,Ip,Name from "),conn);
DataSet ds=new DataSet();
try
{
adapter.Fill(ds,"UserId");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return ds.Tables[0]; }//按钮
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0; excel.Application.Workbooks.Add(true);
DataTable table=GetData();
//将所得到的表的列名,赋值给单元格
foreach(DataColumn col in table1.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
} //同样方法处理数据
foreach(DataRow row in table1.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table1.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//不可见,即后台处理
excel.Visible=true;
}
解决方案 »
- 求sql 语句
- asp.net treeview,双击节点获取其下所有子节点的值!如何实现?求解!多谢!!
- 郁闷死了.... .net framework 4.5不支持xp.....
- static 变量为什么会不定期初始化?
- Gridview自定义列的问题
- vs2005中我想在js中调用WebService,页面该怎么引用或设置?
- 新手求助 GRIDVIEW数据绑定
- 小女不才,望高手指点。。。。。。。。。。。。。。。。
- access中带参数sql语句问题~~标准表达式中数据类型不匹配
- 配置IIS6.0服务器遇到问题,求助!
- 关于DATAGRID按纽列的问题
- 问题依旧没解决,DropDownList的问题!
Object oMissing = System.Reflection.Missing.Value;
Workbook oBook = oExcel.Workbooks.Add( oMissing );
oExcel.Visible = true;
Sheets oSheet = oBook.Worksheets;
_Worksheet worksheet = (_Worksheet) oSheet.get_Item( 1 );oExcel.Cells[ 1, 1 ] = "Title";
int row = 2;
foreach(DataColumn col in table1.Columns)
{
colIndex++;
oExcel.Cells[ row, colIndex + 1 ] = col.ColumnName;
}