導航:首頁 > 文件類型 > winform創建excel文件

winform創建excel文件

發布時間:2025-03-10 00:48:09

『壹』 求用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);

閱讀全文

與winform創建excel文件相關的資料

熱點內容
dpkg如何查看文件路徑 瀏覽:314
設置安卓資源xml的步驟 瀏覽:305
vim怎麼修改文件 瀏覽:683
安卓游戲大型 瀏覽:933
powershell清空文件內容 瀏覽:409
字體在那個文件夾 瀏覽:342
蘋果4真機體驗 瀏覽:551
世界盃買在哪個app 瀏覽:631
魔力寶貝37版本 瀏覽:115
迷你編程的兌換碼是什麼 瀏覽:359
換一個文件櫃玻璃大概多少錢 瀏覽:971
什麼是代理網路 瀏覽:805
axure怎麼發布到手機app 瀏覽:227
如何做動漫視頻教程 瀏覽:331
蘋果公司企業架構 瀏覽:152
順豐錄音文件管理在哪裡 瀏覽:521
象山新建網站製作有哪些步驟 瀏覽:686
什麼app可以查化妝品的日期 瀏覽:174
中國移動香港的網路制式 瀏覽:752
編程在五行中屬什麼 瀏覽:54

友情鏈接