『壹』 求用winform導入導出excel的方法!!!另外我在調試時出錯!!!
在這里補充一下,如果你的電腦安裝了microso offices vs中的操作都好了還是出錯的話,你看看你的offices辦公軟體安裝在c盤,還是其他盤下。我當時遇到的問題是這樣,我的是安裝在d:盤了,結果換了c:盤就好了。
『貳』 求C#(winForm)將數據導出到Execl的實例 要cs文件全部代碼
winForm中導出Execl的方法:
1、方法1:
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
DataTable dt=ds.Tables["table1"];
string name=System.Configuration.ConfigurationSettings.AppSettings["downloarl"].ToString()+DateTime.Today.ToString("yyyyMMdd")+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+".csv";//存放到 web.config中downloarl指定的路徑,文件格式為當前日期+4位隨機數
FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
sw.WriteLine("自動編號,姓名,年齡");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/ms-excel";// 指定返回的是一個不能被客戶端讀取的流,必須被下載
Response.WriteFile(name); // 把文件流發送到客戶端
Response.End();
public void Out2Excel(string sTableName,string url)
{
Excel.Application oExcel=new Excel.Application();
Workbooks oBooks;
Workbook oBook;
Sheets oSheets;
Worksheet oSheet;
Range oCells;
string sFile="",sTemplate="";
//
System.Data.DataTable dt=TableOut(sTableName).Tables[0];
sFile=url+"myExcel.xls";
sTemplate=url+"MyTemplate.xls";
//
oExcel.Visible=false;
oExcel.DisplayAlerts=false;
//定義一個新的工作簿
oBooks=oExcel.Workbooks;
oBooks.Open(sTemplate,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing, Type.Missing);
oBook=oBooks.get_Item(1);
oSheets=oBook.Worksheets;
oSheet=(Worksheet)oSheets.get_Item(1);
//命名該sheet
oSheet.Name="Sheet1";
oCells=oSheet.Cells;
//調用mpdata過程,將數據導入到Excel中去
DumpData(dt,oCells);
//保存
oSheet.SaveAs(sFile,Excel.XlFileFormat.xlTemplate,Type.Missing,Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
oBook.Close(false, Type.Missing,Type.Missing);
//退出Excel,並且釋放調用的COM資源
oExcel.Quit();
GC.Collect();
KillProcess("Excel");
}
private void KillProcess(string processName)
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打開的進程
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if(!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch(Exception Exc)
{
throw new Exception("",Exc);
}
}
『叄』 C#winform 如何導出一定格式的excel啊
1.右擊解決方案的引用,添加.NET中Microsoft.Office.Interop.Excel的引用;
2.在代碼頭添加using Microsoft.Office.Interop.Excel;
3.在函數中添加如下代碼:
//創建
Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook1 = excel1.Workbooks.Add(true);
Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
//寫入
worksheet1.Cells[1, 1] = "航班號";
worksheet1.Cells[1, 2] = "起飛地點";
worksheet1.Cells[1, 3] = "降落地點";
//顯示
excel1.Visible = true;
Console.ReadLine();
//保存文件
workbook1.Close(true, "d:\\1.xls", null);