导航:首页 > 文件类型 > 怎么把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相关的资料

热点内容
飞翔60可以导入PDF文件吗 浏览:832
nilcd程序 浏览:367
手机qq向电脑qq传文件 浏览:613
配置文件不吻合 浏览:489
app品牌怎么推广 浏览:699
哪个版本ps能打开raw文件 浏览:960
h1z1配置文件设置 浏览:212
轻松实现移动硬盘文件夹同步 浏览:876
qq空间传视频文件过大 浏览:285
酷狗音乐下载想要的文件格式 浏览:856
网络推广如何提高rp值 浏览:39
wp8重置后有微信么 浏览:555
iphone4s刷机跳过id 浏览:618
casio手机app去哪里下载 浏览:991
提取txt文件名 浏览:680
e100数据线丢失了怎么办 浏览:248
招聘类网站都有哪些 浏览:625
外网访问内网共享文件夹 浏览:102
linux如何执行sql文件 浏览:31
苹果手机导入文件夹 浏览:41

友情链接