導航:首頁 > 文件類型 > 怎麼把excel文件傳到winform

怎麼把excel文件傳到winform

發布時間:2025-01-24 17:40:22

❶ c# winform程序如何引用excel導出文件

private void BtnExport_Click(object sender, EventArgs e) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.RestoreDirectory = true; saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel文件|*.xls"; saveDialog.FileName = "Sheet1"; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if (saveFileName.IndexOf(":") < 0) return; //點了取消 // Create a new DataTable. System.Data.DataTable table = new System.Data.DataTable("ParentTable"); // Declare variables for DataColumn and DataRow objects. DataColumn column; DataRow row; column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "就診號"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); // Create new DataColumn, set DataType, // ColumnName and add to DataTable. column = new DataColumn(); column.DataType = System.Type.GetType("System.DateTime"); column.ColumnName = "就診時間"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); // Create second column. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "姓名"; column.AutoIncrement = false; column.Caption = "ParentItem"; column.ReadOnly = false; column.Unique = false; // Add the column to the table. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "年齡"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "婚姻狀況"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "文化程度"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "職業"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "固定電話"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "手機"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "電子郵件"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "通信地址"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "病程"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "既往史"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "其它"; column.ReadOnly = true; column.Unique = false; // Add the Column to the DataColumnCollection. table.Columns.Add(column); Authentication au = new Authentication(); PttInfo[] ptInfoArray = au.GetAllPatients(); List<string> marryStateList = new List<string>(); marryStateList.Add("未設"); marryStateList.Add("未婚"); marryStateList.Add("已婚"); marryStateList.Add("離婚"); string[] marryStateArray = marryStateList.ToArray(); List<string> cvStateList = new List<string>(); cvStateList.Add("未設"); cvStateList.Add("文盲"); cvStateList.Add("小學"); cvStateList.Add("初中"); cvStateList.Add("高中"); cvStateList.Add("本科"); cvStateList.Add("碩士"); cvStateList.Add("博士"); cvStateList.Add("博士後"); cvStateList.Add("更高"); string[] cvStateArray = cvStateList.ToArray(); for (int i = 0; i <ptInfoArray.Length; i++) { row = table.NewRow(); row["姓名"] = ptInfoArray[i].name; row["就診號"] = ptInfoArray[i].pttid; row["就診時間"]=ptInfoArray[i].consultdate; row["年齡"] = ptInfoArray[i].age; row["婚姻狀況"]=marryStateArray[ptInfoArray[i].marrystate]; row["文化程度"] = cvStateArray[ptInfoArray[i].cvstate]; row["職業"] = ptInfoArray[i].job; row["固定電話"] = ptInfoArray[i].tel; row["手機"] = ptInfoArray[i].cellphone; row["電子郵件"] = ptInfoArray[i].email; row["通信地址"] = ptInfoArray[i].address; row["病程"] = ptInfoArray[i].ration; row["既往史"] = ptInfoArray[i].diseasehistory; row["其它"] = ptInfoArray[i].other; table.Rows.Add(row); } bool flag = ExportDataTable(table, saveFileName); if (flag) { MessageBox.Show("文件導出成功"); } } public static bool ExportDataTable(System.Data.DataTable dt, string filename) { try { if (filename != "") { if (filename.LastIndexOf(".xls") <= 0) { filename = filename + ".xls"; } if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } Excel.ApplicationClass xlApp = new Excel.ApplicationClass(); if (xlApp == null) { MessageBox.Show("無法創建Excel對象,可能您的機子未安裝Excel"); return false; } Excel.Workbooks workbooks = xlApp.Workbooks; Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1]; for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { if (i == 0) { worksheet.Cells[1, j + 1] = dt.Columns[j].ColumnName; } worksheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString(); } } workbook.Saved = true; workbook.SaveCopyAs(filename); System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); worksheet = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); workbook = null; workbooks.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); workbooks = null; xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; return true; } MessageBox.Show("文件名不能為空!

❷ 如何用C#的winform程序對Excel表格進行增刪修查

這是過去曾參考應用過的方法摘一段給你應急:

一、首先處理好資料庫連接字串

Excel2000-2003: string connStr = "Microsoft.Jet.Oledb.4.0;Data Source='c:\test.xls';Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";

Excel2007: string connStr = "Microsoft.Ace.OleDb.12.0;Data Source='c:\test.xlsx';Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";

其中:

HDR ( Header Row )設置:

若指定值為Yes,代表 Excel 檔中的工作表第一行是欄位名稱

若指定值為 No,代表 Excel 檔中的工作表第一行就是資料了,沒有欄位名稱

IMEX ( IMport EXport mode )設置

當 IMEX=0 時為"匯出模式",這個模式開啟的 Excel 檔案只能用來做"寫入"用途。

當 IMEX=1 時為"匯入模式",這個模式開啟的 Excel 檔案只能用來做"讀取"用途。

當 IMEX=2 時為"連結模式",這個模式開啟的 Excel 檔案可同時支援"讀取"與"寫入"用途。

二、進行表格數據的查詢、插入和更新:

(假設Excel文件text.xls中存在Excel表單tree,有2列分別為id,name)

1、查詢

String sql = "select id, name from [tree$]";



String sql = "select id, name from `tree$`;

2、插入

String sql = "insert into [tree$] (id,name) values(1,'testname');

3、更新

String sql = "update [tree$] set name='name2' where id=1;

4、數據的刪除

在OleDB的連接方式下,不可以使用delete from 語句來刪除某表中的某一條記錄。確切的說,在此模式下,將無法刪除表中的記錄。即使用update語句將所有的欄位寫成null,打開excel文件後依然會發現保留了該空行,而且在使用oleDB連接進行查詢時,依然會查詢到這條空數據。

❸ C# winform 中如何導入Excel

你是要從excel中導入數據到winform嗎?如果是這樣,可以這樣:引用office11.0組件後, Microsoft.Office.Interop.Excel.Application application; //這是一個客戶端
Microsoft.Office.Interop.Excel.Workbooks workbooks; //所有工作薄
Microsoft.Office.Interop.Excel.Worksheet worksheet;//工作表
Microsoft.Office.Interop.Excel.Workbook workbook; //所用到的工作表 void IsRunEX(){ OpenFileDialog openfilediaglog = new OpenFileDialog();
openfilediaglog.Filter = "xls文件|*.xls";
if (openfilediaglog.ShowDialog() == DialogResult.OK)
{
FieldName = openfilediaglog.FileName;
application = new Microsoft.Office.Interop.Excel.Application();
workbooks = application.Workbooks;
workbook = returnworkbook(FieldName, workbooks);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; //選擇第一個表
Range range = worksheet.Cells[1, 8] as Range; //這是選擇第一行第八列的內容
Range rangee = worksheet.Cells[1, 9] as Range; //這是第一行到九列的內容 string str1=range.Value2.ToString(); string str2=ragee.Value2.ToString(); ................... //你所要做的操作 .................. workbook.Close(Type.Missing, FieldName, Type.Missing);
workbooks.Close(); //退出關閉資源
application.Quit();
}
} private Workbook returnworkbook(string filename,Workbooks works) //這里是打一開一個工作表
{
Microsoft.Office.Interop.Excel.Workbook wk=works.Open(
filename, 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);
return wk;
}

❹ 將某個excel文件中的內容顯示在c# winform窗口中

像讀access資料庫一樣啊
select * from sheet1;
讀取後返回一個Dataset
如果在winform窗口中添加一個datagirdview控制項,將專它的datasource設成那個Dataset 就oK了。屬

閱讀全文

與怎麼把excel文件傳到winform相關的資料

熱點內容
輕松實現移動硬碟文件夾同步 瀏覽:876
qq空間傳視頻文件過大 瀏覽:285
酷狗音樂下載想要的文件格式 瀏覽:856
網路推廣如何提高rp值 瀏覽:39
wp8重置後有微信么 瀏覽:555
iphone4s刷機跳過id 瀏覽:618
casio手機app去哪裡下載 瀏覽:991
提取txt文件名 瀏覽:680
e100數據線丟失了怎麼辦 瀏覽:248
招聘類網站都有哪些 瀏覽:625
外網訪問內網共享文件夾 瀏覽:102
linux如何執行sql文件 瀏覽:31
蘋果手機導入文件夾 瀏覽:41
ug編程課哪個最好又便宜 瀏覽:992
蘋果5s拆機後屏幕不亮 瀏覽:680
win10什麼鍵一鍵到桌面圖標 瀏覽:711
用哪個命令查看文件內容比較合適 瀏覽:539
蘋果手機怎麼玩nba2k14 瀏覽:773
資料庫插入數據怎麼弄 瀏覽:83
windows2008密碼策略 瀏覽:953

友情鏈接