導航:首頁 > 文件類型 > 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文件相關的資料

熱點內容
win7列印機程序服務沒有運行 瀏覽:972
網站如何花唄支付戰網 瀏覽:609
win10刷新速率 瀏覽:946
建設u盾密碼幾位數 瀏覽:189
伴奏文件是什麼 瀏覽:722
財政預算代碼在哪裡找 瀏覽:123
怎麼改文件夾時間 瀏覽:116
酷狗761版本下載 瀏覽:267
iphone6微雲在哪裡 瀏覽:336
dnf3月qq會員活動 瀏覽:89
西門子plc編程如何加網格線 瀏覽:590
微雲自動上傳文件 瀏覽:862
芒果錢包app下載 瀏覽:860
json數組轉為list集合 瀏覽:71
什麼是網路加 瀏覽:969
qq瀏覽器手機版java下載 瀏覽:598
如何在手機上申訴qq號 瀏覽:258
win10用天正建築 瀏覽:929
截屏它是哪個文件 瀏覽:820
登山賽車2升級 瀏覽:67

友情鏈接