求数据导出到excel方法

Neky 2004-06-28 11:26:21
要是excel表不存在则自动生成excel文件并将数据导入到excel
各位高手有什么好的方法?
...全文
174 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Neky 2004-06-28
  • 打赏
  • 举报
回复
请问有具体介绍的篇子吗?
jiezhi 2004-06-28
  • 打赏
  • 举报
回复
一个简单的:
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Application myExcel = (Excel.Application)(new Excel.ApplicationClass());
myExcel.Application.Workbooks.Add(true);
myExcel.Visible = true;
// Title
myExcel.Cells[1,4] = txtTitle.Text;

//
string strField = "";
for(int i=0;i<listView.Items.Count;i++)
{
if(listView.Items[i].Checked)
{
strField = strField+listView.Items[i].Text + ",";
}
}
string strFields = "";
strFields = "," + strField;
int intRow = 3;
bool b = false;
foreach(DataRow dr in _PubDs.Tables[0].Rows)
{
int intCol = 1;
foreach(DataColumn dc in _PubDs.Tables[0].Columns)
{
if(strFields.IndexOf("," + dc.ColumnName + ",")>=0)
{
if(!b)
myExcel.Cells[2,intCol] = dc.ColumnName;
myExcel.Cells[intRow,intCol] = dr[dc.ColumnName].ToString();
intCol++;
}
}
b = true;
intRow++;
}

//myExcel.Save((object)(txtFile.Text));
}
jiezhi 2004-06-28
  • 打赏
  • 举报
回复
搜索,论坛上例子很多。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧