We often read or write a file(.txt, .xls etc.), and we can achieve it through following common class.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using Excel;
using System.Runtime.InteropServices;
namespace OF_Common
{
public class fileHelper
{
public string readLabelFile(string strFilePathName)
{
string strLabel="";
string strLine = "";
try
{
FileStream file = new FileStream(strFilePathName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file, Encoding.UTF8);
while ((strLine = sr.ReadLine()) != null)
{
if (strLine != "")
{
strLabel = strLabel + strLine;
}
}
sr.Close();
file.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return strLabel;
}
public string readFromFile(string strFilePathName)
{
string strContent = "";
StreamReader sr = null;
Encoding ecType = Encoding.Default;
try
{
sr = new StreamReader(strFilePathName, ecType);
strContent = sr.ReadToEnd();
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
finally
{
sr.Close();
}
return strContent;
}
public bool WriteFile(string strFilePath, string strContent, bool bolOverWrite)
{
Encoding ecType = Encoding.Default;
bool bolResult = true;
StreamWriter sw = null;
try
{
sw = new StreamWriter(strFilePath, !bolOverWrite, ecType);
sw.Write(strContent);
}
catch (Exception Ex)
{
throw new Exception(Ex.Message, Ex);
}
finally
{
sw.Close();
}
return bolResult;
}
public void genDTExcel(string strFilePath, System.Data.DataTable dt)
{
try
{
Excel.Application xlApp = new Excel.ApplicationClass();
int rowIndex;
int colIndex;
rowIndex = 1;
colIndex = 0;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
xlApp.Visible = false;
if (dt.Rows.Count > 0)
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, dt.Columns.Count]);
range.Font.Size = 9;//单元字体大小
range.Font.Bold = true;
range.Font.Color = System.Drawing.Color.White.ToArgb(); ;//单元字体顔色
range.Interior.Color = System.Drawing.Color.MediumBlue.ToArgb();//单元背景
range.Font.Name = "Arial";//单元字体字体名称
range.Borders.LineStyle. = 1;
Excel.Range range1 = xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[dt.Rows.Count + 1, dt.Columns.Count]);
range1.Font.Size = 9;//单元字体大小
range1.Font.Color = System.Drawing.Color.Black.ToArgb(); ;//单元字体顔色
range1.Interior.Color = System.Drawing.Color.LemonChiffon.ToArgb();//单元背景
range1.Font.Name = "Arial";//单元字体字体名称
range1.Borders.LineStyle. = 1;
foreach (DataColumn colu in dt.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[1, colIndex] = colu.ColumnName;
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < dt.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < dt.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = dt.Rows[row][col].ToString();
}
}
}
else
{
System.Windows.Forms.Application.Exit();
}
xlApp.Cells.EntireColumn.AutoFit();
Excel.Range range2 = xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[dt.Rows.Count + 1, 1]);
range2.Font.Size = 9;//单元字体大小
range2.Font.Color = System.Drawing.Color.Black.ToArgb(); ;//单元字体颜色
range2.Interior.Color = System.Drawing.Color.LemonChiffon.ToArgb();//单元背景
range2.Font.Name = "Arial";
range2.Borders.LineStyle. = 1;
//range2.NumberFormatLocal = "@";
range2.NumberFormatLocal = "###############";
Excel.Range range3 = xlApp.get_Range(xlApp.Cells[2, 2], xlApp.Cells[dt.Rows.Count + 1, dt.Columns.Count]);
range3.Font.Size = 9;//单元字体大小
range3.Font.Color = System.Drawing.Color.Black.ToArgb(); ;//单元字体颜色
range3.Interior.Color = System.Drawing.Color.LemonChiffon.ToArgb();//单元背景
range3.Font.Name = "Arial";
range3.Borders.LineStyle. = 1;
range3.NumberFormatLocal = "@";
xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
xlBook.SaveAs(strFilePath + DateTime.Now.ToString("HHmmss") + ".XLS", Excel.XlFileFormat.xlXMLSpreadsheet, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
xlApp.Application.Workbooks.Close();
xlApp.Application.Quit();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
GC.Collect(0);
}
catch (Exception e)
{
throw e;
}
}
public void genGeneralExcel(string strFilePath, System.Data.DataTable dt)
{
try
{
string header = "";
string items = "";
string strPath = strFilePath +"_"+ DateTime.Now.ToString("HHmmss") + ".XLS";
StreamWriter sw = new StreamWriter(strPath, true);
foreach (DataColumn colu in dt.Columns)
{
header += colu.ColumnName.Trim('\t') + '\t';
}
header = header.TrimEnd('\t');
sw.WriteLine(header);
for (int row = 0; row < dt.Rows.Count; row++)
{
items = "";
for (int col = 0; col < dt.Columns.Count; col++)
{
items += dt.Rows[row][col].ToString().Trim('\r').Trim('\n').Trim('\t') + '\t';
}
items = items.TrimEnd('\t');
sw.WriteLine(items);
}
sw.Close();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15053694/viewspace-442080/ ,如需转载,请注明出处,否则将追究法律责任。