using System;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
//using Microsoft.Office.Interop.Excel;
namespace CarSystem.Account.FileUtil
{
///
/// 模块编号:PTS.Common.ExcelUtil
/// 作 用:Excel读写类
/// 编写日期:2009-09-08
///
public class ExcelUtil
{
//excel数据类型
private const char SEPARAT = '#';
private const string DATA_TYPE_STRING = "String";
private const string DATA_TYPE_NUMBER = "Number";
//excel命名空间
private static string urlSS;
//解析生成的结果
private static XmlDocument xmlDoc;
static ExcelUtil()
{
//实例化一个XmlDocument对像
xmlDoc = new XmlDocument();
urlSS = "urn:schemas-microsoft-com:office:spreadsheet";
}
public enum DataType
{
String,
Number
}
///
/// 取得用于输出到excel的合并字符
///
///
///
///
public static string GetJoinedData(DataType type, object data)
{
#region
try
{
if (data == null)
{
return string.Empty;
}
if (DataType.String == type)
{
return DATA_TYPE_STRING + SEPARAT + data.ToString();
}
else if (DataType.Number == type)
{
return DATA_TYPE_NUMBER + SEPARAT + data.ToString();
}
else
{
return DATA_TYPE_STRING + SEPARAT + data.ToString();
}
}
catch { return ""; }
#endregion
}
///
/// 取得用于输出到excel的合并字符
///
///
///
///
public static string GetJoinedData(DataType type, decimal? data, string format)
{
#region
try
{
if (DataType.String == type)
{
return DATA_TYPE_STRING + SEPARAT + data.GetValueOrDefault().ToString(format);
}
else if (DataType.Number == type)
{
return DATA_TYPE_NUMBER + SEPARAT + data.GetValueOrDefault() + SEPARAT + format;
}
else
{
return DATA_TYPE_STRING + SEPARAT + data.GetValueOrDefault().ToString(format);
}
}
catch { return ""; }
#endregion
}
///
/// 单元格类型
///
///
///
private enum CellType
{
Data,
StationName,
Legend
}
#region 获取模板
///
/// 写入Excel
///
///
/// 默认文件名
/// 写入内容
public static void WriteExcel_(string filePathName, string defaultSheetName, List ls)
{
#region
//超过excel最大行数
if (ls.Count > 65536)
{
throw new Exception("导出数据不能超过65536条!\n");
return;
}
//*****文件另存为*****
StreamWriter sw = new StreamWriter(filePathName, false, System.Text.Encoding.UTF8);
StreamWriter sw2 = sw;
StringBuilder sbContext = new StringBuilder();
sw2.WriteLine("");
sw2.WriteLine("");
sw2.WriteLine("");
sw2.WriteLine("");
sw2.WriteLine(" ");
sw2.WriteLine("");
int pageSize = 100;//每页600条,计算有几页
int cnt = (ls.Count - 1) / pageSize + ((ls.Count - 1) <= pageSize ? 1 : ((ls.Count - 1) % pageSize > 0 ? 1 : 0));
for (int pg = 1; pg <= cnt; pg++)
{
//sheet名
sw2.WriteLine("");
sw2.WriteLine(" ");
#region 打印标题
sw2.WriteLine(" ");
for (int col = 0; col < ls[1].Length; col++)
{
//数据为空
if (string.IsNullOrEmpty(ls[0][col]))
{
sw2.WriteLine(" | ");
continue;
}
var aryData = ls[0][col].Split(SEPARAT);
if (aryData.Length == 1)
{
sw2.WriteLine(" " + aryData[0] + " | ");
}
else if (aryData.Length == 2)
{
sw2.WriteLine(" " + aryData[1] + " | ");
}
else if (aryData.Length == 3)
{
sw2.WriteLine(" " + aryData[1] + " | ");
}
}
sw2.WriteLine("
");
#endregion
for (int row = (pg - 1) * pageSize + 1; row < pg * pageSize + 1; row++)
{
if (row >= ls.Count) { break; } //当达到最大记录数,则退出循环
sw2.WriteLine(" ");
for (int col = 0; col < ls[row].Length; col++)
{
//数据为空
if (string.IsNullOrEmpty(ls[row][col]))
{
sw2.WriteLine(" | ");
continue;
}
var aryData = ls[row][col].Split(SEPARAT);
if (aryData.Length == 1)
{
sw2.WriteLine(" " + aryData[0] + " | ");
}
else if (aryData.Length == 2)
{
sw2.WriteLine(" " + aryData[1] + " | ");
}
else if (aryData.Length == 3)
{
sw2.WriteLine(" " + aryData[1] + " | ");
}
}
sw2.WriteLine("
");
}
sw2.WriteLine("
");
sw2.WriteLine("");
}
sw2.WriteLine("");
//*****文件另存为*****
sw2 = null;
sw.Close();
sw.Dispose();
//********************
// RepairXls(filePathName);
#endregion
}
///
/// 写入Excel
///
///
/// 默认文件名
/// 写入内容
public static void WriteExcel(string filePathName, string defaultSheetName, List ls)
{
#region
//超过excel最大行数
if (ls.Count > 65536)
{
throw new Exception("导出数据不能超过65536条!\n");
return;
}
//*****文件另存为*****
StreamWriter sw = new StreamWriter(filePathName, false, System.Text.Encoding.UTF8);
StreamWriter sw2 = sw;
StringBuilder sbContext = new StringBuilder();
sw2.WriteLine("");
sw2.WriteLine("");
sw2.WriteLine("");
sw2.WriteLine("");
sw2.WriteLine(" ");
sw2.WriteLine("");
//sheet名
sw2.WriteLine("");
sw2.WriteLine(" ");
for (int row = 0; row < ls.Count; row++)
{
sw2.WriteLine(" ");
for (int col = 0; col < ls[row].Length; col++)
{
//数据为空
if (string.IsNullOrEmpty(ls[row][col]))
{
sw2.WriteLine(" | ");
continue;
}
var aryData = ls[row][col].Split(SEPARAT);
if (aryData.Length == 1)
{
sw2.WriteLine(" " + aryData[0] + " | ");
}
else if (aryData.Length == 2)
{
sw2.WriteLine(" " + aryData[1] + " | ");
}
else if (aryData.Length == 3)
{
sw2.WriteLine(" " + aryData[1] + " | ");
}
}
sw2.WriteLine("
");
}
sw2.WriteLine("
");
sw2.WriteLine("");
sw2.WriteLine("");
//*****文件另存为*****
sw2 = null;
sw.Close();
sw.Dispose();
//********************
// RepairXls(filePathName);
#endregion
}
///
/// 把 Xml 格式的 Excel 文件转换成 二进制格式的 Excel 文件
/// 2011-07-03 李峰
///
///
//public static void RepairXls(string xlsPath)
//{
// #region
// Workbook excelBook = null;
// ApplicationClass excelApp = null;
// object Nothing = System.Reflection.Missing.Value;
// excelApp = new ApplicationClass();
// excelApp.Visible = false;
// excelApp.DisplayAlerts = false;
// excelApp.AlertBeforeOverwriting = false;
// excelApp.AskToUpdateLinks = false;
// excelBook = excelApp.Workbooks.Open(xlsPath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
// try
// {
// excelBook.SaveAs(xlsPath, XlFileFormat.xlExcel5, Nothing, Nothing, Nothing, Nothing, XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);
// }
// catch (Exception ex)
// { }
// if (excelBook != null)
// {
// excelBook.Close(Nothing, Nothing, Nothing);
// System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook);
// excelBook = null;
// excelApp.Application.Quit();
// System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
// excelApp = null;
// }
// GC.Collect();
// GC.WaitForPendingFinalizers();
// #endregion
//}
#endregion
}
}