遇到个问题 代码:excelApp.Workbooks.Add(System.Windows.Forms.Application.StartupPath + @"/金相-常规.xls"); 明明指定了具体位置 可导的时候还是新开了个excel 好奇怪啊 为什么啊 怎样解决啊 我想导倒已经存在的excel中啊
vb里 直接open 就可以了 c#中 我试了open 还是不行 求求各位大侠 帮帮我
vb里 直接open 就可以了 c#中 我试了open 还是不行 求求各位大侠 帮帮我
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
回3楼:不是\的问题
回4楼:直接打开?怎样打开呢?
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + @"/金相-常规.xls", Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//excelApp.Workbooks.Add(System.Windows.Forms.Application.StartupPath + @"\金相-常规.xls");
Excel.Workbook excelBook = excelApp.Workbooks.Add(Type.Missing);
Worksheet excelSheet = (Worksheet)excelApp.ActiveSheet;
//excelSheet.Name = "Abc";
excelApp.Visible = true;
int rowCount = Table1.Rows.Count;
int colCount = Table1.Columns.Count; object[,] dataArray = new object[rowCount, colCount];//二维数组定义是多一个标题行。
for (int j = 0; j < colCount; j++)
{
dataArray[0, j] = Table1.Columns[j].Caption;//导出字段标题。
//根据各列的数据类型设置Excel的格式。
switch (Table1.Columns[j].DataType.ToString())//格式化单元格的输出格式
{
case "System.String":
excelSheet.get_Range(excelSheet.Cells[1, 1 + j], excelSheet.Cells[rowCount + 1, 1 + j]).NumberFormatLocal = "@";
break;
case "System.Decimal":
excelSheet.get_Range(excelSheet.Cells[1, 1 + j], excelSheet.Cells[rowCount + 1, 1 + j]).NumberFormatLocal = "$0.00";
break;
case "System.DateTime":
excelSheet.get_Range(excelSheet.Cells[1, 1 + j], excelSheet.Cells[rowCount + 1, 1 + j]).NumberFormatLocal = "yyyy-mm-dd";
break;
//可以根据自己的需要扩展。
default:
excelSheet.get_Range(excelSheet.Cells[1, 1 + j], excelSheet.Cells[rowCount + 1, 1 + j]).NumberFormatLocal = "G/通用格式";
break;
}
for (int i = 0; i < rowCount; i++)
{
dataArray[i, j] = Table1.Rows[i][j];
}
}
excelSheet.get_Range("A1", excelSheet.Cells[rowCount, colCount]).Value2 = dataArray;
//excelSheet.SaveAs(System.Windows.Forms.Application.StartupPath + @"\金相-常规.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); if (excelSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet);
excelSheet = null;
}
excelBook.Close(Missing.Value, Missing.Value, Missing.Value);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook);
excelApp = null;
GC.Collect();
//excelApp.Quit();
//excelApp = null;
代码全在这了 各位大哥 open我也试过了 同样的效果 帮我想想吧
1、将整个html全部输出Excel此法将html中所有的内容,如按钮,表格,图片等全部输出到Excel中。 Response.Clear();
Response.Buffer= true;
Response.AppendHeader("Content-Disposition","attachment;filename="+DateTime.Now.ToString("yyyyMMdd")+".xls");
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false; 这里我们利用了ContentType属性,它默认的属性为text/html,这时将输出为超文本,即我们常见的网页格式到客户端,如果改为ms-excel将将输出excel格式,也就是说以电子表格的格式输出到客户端,这时浏览器将提示你下载保存。ContentType的属性还包括:image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 。同理,我们也可以输出(导出)图片、word文档等。下面的方法,也均用了这个属性。2、将DataGrid控件中的数据导出Excel上述方法虽然实现了导出的功能,但同时把按钮、分页框等html中的所有输出信息导了进去。而我们一般要导出的是数据,DataGrid控件上的数据。 System.Web.UI.Control ctl=this.DataGrid1;
//DataGrid1是你在窗体中拖放的控件
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
如果你的DataGrid用了分页,它导出的是当前页的信息,也就是它导出的是DataGrid中显示的信息。而不是你select语句的全部信息。为方便使用,写成方法如下: public void DGToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
} 用法:DGToExcel(datagrid1);
3、将DataSet中的数据导出Excel有了上边的思路,就是将在导出的信息,输出(Response)客户端,这样就可以导出了。那么把DataSet中的数据导出,也就是把DataSet中的表中的各行信息,以ms-excel的格式Response到http流,这样就OK了。说明:参数ds应为填充有数据表的DataSet,文件名是全名,包括后缀名,如Excel2006.xls
public void CreateExcel(DataSet ds,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename="+FileName);
string colHeaders= "", ls_item="";
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i=0;
int cl=dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for(i=0;i<cl;i++)
{
if(i==(cl-1))//最后一列,加n
{
colHeaders +=dt.Columns[i].Caption.ToString() +"n";
}
else
{
colHeaders+=dt.Columns[i].Caption.ToString()+"t";
}
}
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach(DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for(i=0;i<cl;i++)
{
if(i==(cl-1))//最后一列,加n
{
ls_item +=row[i].ToString()+"n";
}
else
{
ls_item+=row[i].ToString()+"t";
}
}
resp.Write(ls_item);
ls_item="";
}
resp.End();
}
.....................................
二、winForm中导出Excel的方法:
.......................................
更多信息请参考:http://wguorun.blog.163.com/blog/static/1630623820077822132632/