winform导出Excel

gtiroy 2008-12-05 09:37:01
将Winform的表单导入到Excel。

界面类似于移动缴费单的形式,要整体导入。
...全文
1636 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiguangfeia 2012-10-15
  • 打赏
  • 举报
回复
学习了
  • 打赏
  • 举报
回复
public void OutExcel()
{
string excelstring="编号,姓名,部门,发送量\r\n111,张三,销售,10000";
string fileName = "";
saveFileDialog1.DefaultExt = "csv";
saveFileDialog1.Filter = "Excel文件|*.csv";
saveFileDialog1.FileName = "短信量";
DialogResult r = saveFileDialog1.ShowDialog();
if (r!= DialogResult.Cancel)
{
fileName = saveFileDialog1.FileName;
}
if (fileName.Trim()=="")
{
return;
}

if (!File.Exists(fileName))
{
FileStream fs = File.Create(fileName);
fs.Close();
}
System.IO.StreamWriter writer = new StreamWriter(fileName, false, Encoding.GetEncoding("GB2312"));
writer.Write(excelstring);
writer.Close();
}
一桶江湖 2011-04-29
  • 打赏
  • 举报
回复
吼吼,色狼出动了
moshuyouke 2011-03-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hdngo 的回复:]
C# code
protected void AddExcel(DataSet ds)
{
DataTable dt = ds.Tables[0];

string fileName = Guid.NewGuid() + ".xls";

Excel.Application excel = new Excel.ApplicationClass();

int rowIn……
[/Quote] 这是不是少了保存路径???
kiqq2003 2011-03-30
  • 打赏
  • 举报
回复
学习啦~~~感谢指点~
liangle840 2011-01-17
  • 打赏
  • 举报
回复
看看,顶~~~~~
jinsuo_1986 2008-12-05
  • 打赏
  • 举报
回复
学习
tellov 2008-12-05
  • 打赏
  • 举报
回复
我建议直接用vs自带的报表功能,不仅能导出excel,还能导出pdf.
wuyq11 2008-12-05
  • 打赏
  • 举报
回复
参考
http://blog.csdn.net/zczc198401/archive/2008/02/14/2096089.aspx
http://www.cnblogs.com/Ihaveadream/archive/2008/08/17/1269960.html
zhnzzy 2008-12-05
  • 打赏
  • 举报
回复
简单的就用用这个吧

using Excel;

string FPath=@System.Windows.Forms.Application.StartupPath+"\\AAA.xls";
Excel.ApplicationClass ex=new Excel.ApplicationClass();
ex.Application.Workbooks.Add(FPath);
ex.Cells[1,1]="赋值";
ex.Visible=true;
tianshangfei 2008-12-05
  • 打赏
  • 举报
回复
学习~
patrickpan 2008-12-05
  • 打赏
  • 举报
回复
这个是导出DataSet到Excel的,改造一下,就可以了。
1.引用 COM组件 Excel.dll

2.实现如下

public void ExportExcel(){
DataSet ds=this.DataSet;//取得dataGrid绑定的DataSet
if(ds==null) return;

string saveFileName="";
bool fileSaved=false;
SaveFileDialog saveDialog=new SaveFileDialog();
saveDialog.DefaultExt ="xls";
saveDialog.Filter="Excel文件|*.xls";
saveDialog.FileName ="Sheet1";
saveDialog.ShowDialog();
saveFileName=saveDialog.FileName;
if(saveFileName.IndexOf(":")<0) return; //被点了取消

Excel.Application xlApp=new Excel.Application();

if(xlApp==null){
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
return;
}

Excel.Workbooks workbooks=xlApp.Workbooks;
Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Excel.Range range;

string oldCaption=this.CaptionText;
long totalCount=ds.Tables[0].Rows.Count;
long rowRead=0;
float percent=0;

worksheet.Cells[1,1]=this.CaptionText;
//写入字段
for(int i=0;i<ds.Tables[0].Columns.Count;i++){
worksheet.Cells[2,i+1]=ds.Tables[0].Columns[i].ColumnName;
range=(Excel.Range)worksheet.Cells[2,i+1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;

}
//写入数值
this.CaptionVisible = true;
for(int r=0;r<ds.Tables[0].Rows.Count;r++){
for(int i=0;i<ds.Tables[0].Columns.Count;i++){
worksheet.Cells[r+3,i+1]=ds.Tables[0].Rows[r][i];
}
rowRead++;
percent=((float)(100*rowRead))/totalCount;
this.CaptionText = "正在导出数据["+ percent.ToString("0.00") +"%]...";
Application.DoEvents();
}
this.CaptionVisible = false;
this.CaptionText = oldCaption;

range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds.Tables[0].Rows.Count+2,ds.Tables[0].Columns.Count]);
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);

range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;

if(ds.Tables[0].Columns.Count>1){
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
}

if(saveFileName!=""){
try{
workbook.Saved =true;
workbook.SaveCopyAs(saveFileName);
fileSaved=true;
}catch(Exception ex){
fileSaved=false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n"+ex.Message);
}
}else{
fileSaved=false;
}
xlApp.Quit();
GC.Collect();//强行销毁
if(fileSaved && File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName);

}
zhnzzy 2008-12-05
  • 打赏
  • 举报
回复
以前写过一个导入DLL,
HDNGO 2008-12-05
  • 打赏
  • 举报
回复
protected void AddExcel(DataSet ds) 
{
DataTable dt = ds.Tables[0];

string fileName = Guid.NewGuid() + ".xls";

Excel.Application excel = new Excel.ApplicationClass();

int rowIndex = 1;
int colIndex = 0;

excel.Application.Workbooks.Add(true);

foreach (DataColumn col in dt.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}

foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 0;
for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
{
excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
}
}

excel.Visible = false;
excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
//excel.Save(fileName);

excel.Quit();
excel = null;

GC.Collect();//垃圾回收
}

110,534

社区成员

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

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

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