txt Excel 读写
来源:互联网 发布:下载软件大全 编辑:程序博客网 时间:2024/06/05 14:31
http://www.51zxw.net/study.asp?vip=11181777]51自学网-专业培训老师录制的视频教程,让学习变得很轻松
</pre><pre code_snippet_id="130479" snippet_file_name="blog_20131227_1_5257510" name="code" class="csharp">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Data;using Microsoft.Office.Interop.Excel;using System.Windows.Forms;using System.Data.OleDb;using System.Reflection;namespace util{ class FileUtil { /// <summary> /// 当前路径。 /// </summary> public string currPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); /// <summary> /// 写入TXT /// </summary> /// <param name="fileName">文件名</param> /// <param name="listView">文件内容 list(string)</param> /// <returns></returns> public static bool WriteToFile(string fileName, List<string> listView) { StreamWriter SW = null; try { string directory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Directory.GetCurrentDirectory(); // MessageBox.Show(directory); directory = directory.Replace("file:\\", ""); // MessageBox.Show(directory + "\\" + fileName); bool isExist = File.Exists(fileName); //MessageBox.Show(isExist+""); if (isExist) {//文件是否存在。 SW = new StreamWriter(directory + "\\" + fileName, true); //SW = System.IO.File.AppendText(directory + "\\" + fileName); } else { SW = System.IO.File.CreateText(directory + "\\" + fileName); } foreach (string s in listView) { SW.WriteLine(s); } SW.Close(); return true; } catch (System.Exception e) { MessageBox.Show("保存文件失败!" + e); return false; } finally { SW.Close(); } } /// <summary> /// ListView反向填充DataTable数据项 /// </summary> /// <param name="lv">ListView控件</param> /// <param name="dt">DataTable</param> public static System.Data.DataTable listViewToDataTable(ListView lv) { if(lv.Items.Count==0){ return null; } System.Data.DataTable dt = new System.Data.DataTable(); DataRow dr; dt.Clear(); dt.Columns.Clear(); for (int k = 0; k < lv.Columns.Count; k++) { dt.Columns.Add(lv.Columns[k].Text.Trim().ToString());//生成DataTable列头 } for (int i = 0; i < lv.Items.Count; i++) { dr = dt.NewRow(); for (int j = 0; j < lv.Columns.Count; j++) { dr[j] = lv.Items[i].SubItems[j].Text.Trim(); } dt.Rows.Add(dr);//每行内容 } return dt; } public static System.Data.DataTable readExcel(String filePath)//更新中********************** { Microsoft.Office.Interop.Excel.Workbook wb = null; Microsoft.Office.Interop.Excel.Worksheet ws = null; Microsoft.Office.Interop.Excel.Range r = null; object missing = Missing.Value; System.Data.DataTable data = new System.Data.DataTable(); ApplicationClass app = new ApplicationClass(); wb = app.Workbooks.Open(filePath, false, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); app.Visible = false;//读Excel不显示出来影响用户体验 //得到WorkSheet对象 ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1); int i = 1; //读取A1单元格内容 while (ws.get_Range("A"+i, Type.Missing)!=null) { r = ws.get_Range("A" + i, Type.Missing); if(r.Value2 == null ){ break; } string strA1 = r.Value2.ToString(); MessageBox.Show(strA1); i++; } app.Quit();//退出 return data; } /// <summary> /// 保存Excel /// </summary> /// <param name="excelTable"></param> /// <param name="filePath"></param> /// <returns></returns> public static bool writeFileToExcel(System.Data.DataTable excelTable, string filePath) { if(excelTable ==null){ MessageBox.Show("列表内容为空!"); return false; } ApplicationClass app = new ApplicationClass(); try { app.Visible = false;//不显示 Excel 文件,如果为 true 则显示 Excel 文件 Workbook wBook = app.Workbooks.Add(true);//添加一个工作薄 Worksheet wSheet = wBook.Worksheets[1] as Worksheet;//添加一个工作表 int size = excelTable.Columns.Count;//列 表格台头 for (int i = 0; i < size; i++) { wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName; } //表格内容 if (excelTable.Rows.Count > 0) { int row = 0; row = excelTable.Rows.Count;//行 int col = excelTable.Columns.Count;//列 for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { string str = excelTable.Rows[i][j].ToString(); wSheet.Cells[i + 2, j + 1] = str; } } } //设置禁止弹出保存和覆盖的询问提示框 app.DisplayAlerts = false; app.AlertBeforeOverwriting = false; //保存工作簿 wBook.Save(); //保存excel文件 app.Save(filePath); app.SaveWorkspace(filePath); app.Quit(); app = null; return true; } catch (Exception err) { MessageBox.Show("导出Excel出错!错误原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } finally { } } /// <summary> /// 读取文件 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static List<string> readFileToList(String fileName) { List<string> list = new List<string>(); if (File.Exists(fileName)) { StreamReader setReader = new StreamReader(fileName); string str; while ((str = setReader.ReadLine()) != null) { list.Add(str); } setReader.Close(); } return list; } /// <summary> /// 删除文件 /// </summary> /// <param name="fileName"></param> public static void removeFile(String fileName) { if (File.Exists(fileName)) { File.Delete(fileName); } } internal static void WriteToFile(string p, object p_2) { throw new NotImplementedException(); } }}
0 0
- txt Excel 读写
- SAP ABAP读写Excel 与 TXT文件
- Matlab txt,excel文件的读写
- C# 1读写Excel 2创建文件读写入txt
- socket连接,传输文件,读写excel,txt,多线程处理请求
- excel-txt类型数据读写-TitanTabTools.cs-连载1
- excel-txt类型数据读写-RWInterface.cs-连载1
- excel-txt类型数据读写-测试与结果
- VB读写TXT文件
- txt文件读写
- java 读写txt文本文件
- IO读写修改txt
- C#读写txt文件
- Java读写txt文件
- txt文件读写
- bcb 读写txt
- .NET读写txt
- .Txt 文件读写
- 间歇性向上无缝滚动(jq插件封装型)
- 展讯平台驱动学习心得(一)—心得概述
- java程序员面试基础知识
- 基础测试题 数字转换
- ArcGIS制图表达—突出实体目标
- txt Excel 读写
- 番茄时间管理法(Pomodoro Technique):一个番茄是如何让你工作更有效率的
- Java内存分配工作原理
- 常用算法经典代码(C++版)
- 在微信公众平台前端网页上添加分享按钮
- JBPM学习__六个Service API
- ubuntu12.04循环登录,无法进桌面的问题
- ROMAN NUMERALS 题解
- 生产者消费者问题