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();
}
}
}