天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

369 lines
14 KiB

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
{
///<summary>
/// 模块编号:PTS.Common.ExcelUtil
/// 作 用:Excel读写类
/// 编写日期:2009-09-08
///</summary>
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
}
/// <summary>
/// 取得用于输出到excel的合并字符
/// </summary>
/// <param name="type"></param>
/// <param name="data"></param>
/// <returns></returns>
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
}
/// <summary>
/// 取得用于输出到excel的合并字符
/// </summary>
/// <param name="type"></param>
/// <param name="data"></param>
/// <returns></returns>
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
}
/// <summary>
/// 单元格类型
/// </summary>
/// <param name="RowNumber"></param>
/// <returns></returns>
private enum CellType
{
Data,
StationName,
Legend
}
#region 获取模板
/// <summary>
/// 写入Excel
/// </summary>
/// <param name="response"></param>
/// <param name="defaultFileName">默认文件名</param>
/// <param name="ls">写入内容</param>
public static void WriteExcel_(string filePathName, string defaultSheetName, List<string[]> 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("<?xml version=\"1.0\"?>");
sw2.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw2.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw2.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw2.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sw2.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw2.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw2.WriteLine("<Styles>");
sw2.WriteLine(" <Style ss:ID=\"s21\"><NumberFormat ss:Format=\"0.00_ \"/></Style>");
sw2.WriteLine("</Styles>");
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("<Worksheet ss:Name=\"" + defaultSheetName + pg.ToString() + "\">");
sw2.WriteLine(" <Table>");
#region 打印标题
sw2.WriteLine(" <Row ss:Index=\"" + (1) + "\">");
for (int col = 0; col < ls[1].Length; col++)
{
//数据为空
if (string.IsNullOrEmpty(ls[0][col]))
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"String\"></Data></Cell>");
continue;
}
var aryData = ls[0][col].Split(SEPARAT);
if (aryData.Length == 1)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"String\">" + aryData[0] + "</Data></Cell>");
}
else if (aryData.Length == 2)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"" + aryData[0] + "\">" + aryData[1] + "</Data></Cell>");
}
else if (aryData.Length == 3)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\" ss:StyleID=\"s21\"><Data ss:Type=\"" + aryData[0] + "\">" + aryData[1] + "</Data></Cell>");
}
}
sw2.WriteLine(" </Row>");
#endregion
for (int row = (pg - 1) * pageSize + 1; row < pg * pageSize + 1; row++)
{
if (row >= ls.Count) { break; } //当达到最大记录数,则退出循环
sw2.WriteLine(" <Row ss:Index=\"" + (row - (pg - 1) * pageSize + 1) + "\">");
for (int col = 0; col < ls[row].Length; col++)
{
//数据为空
if (string.IsNullOrEmpty(ls[row][col]))
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"String\"></Data></Cell>");
continue;
}
var aryData = ls[row][col].Split(SEPARAT);
if (aryData.Length == 1)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"String\">" + aryData[0] + "</Data></Cell>");
}
else if (aryData.Length == 2)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"" + aryData[0] + "\">" + aryData[1] + "</Data></Cell>");
}
else if (aryData.Length == 3)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\" ss:StyleID=\"s21\"><Data ss:Type=\"" + aryData[0] + "\">" + aryData[1] + "</Data></Cell>");
}
}
sw2.WriteLine(" </Row>");
}
sw2.WriteLine(" </Table>");
sw2.WriteLine("</Worksheet>");
}
sw2.WriteLine("</Workbook>");
//*****文件另存为*****
sw2 = null;
sw.Close();
sw.Dispose();
//********************
// RepairXls(filePathName);
#endregion
}
/// <summary>
/// 写入Excel
/// </summary>
/// <param name="response"></param>
/// <param name="defaultFileName">默认文件名</param>
/// <param name="ls">写入内容</param>
public static void WriteExcel(string filePathName, string defaultSheetName, List<string[]> 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("<?xml version=\"1.0\"?>");
sw2.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw2.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw2.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw2.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sw2.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw2.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw2.WriteLine("<Styles>");
sw2.WriteLine(" <Style ss:ID=\"s21\"><NumberFormat ss:Format=\"0.00_ \"/></Style>");
sw2.WriteLine("</Styles>");
//sheet名
sw2.WriteLine("<Worksheet ss:Name=\"" + defaultSheetName + "\">");
sw2.WriteLine(" <Table>");
for (int row = 0; row < ls.Count; row++)
{
sw2.WriteLine(" <Row ss:Index=\"" + (row + 1) + "\">");
for (int col = 0; col < ls[row].Length; col++)
{
//数据为空
if (string.IsNullOrEmpty(ls[row][col]))
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"String\"></Data></Cell>");
continue;
}
var aryData = ls[row][col].Split(SEPARAT);
if (aryData.Length == 1)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"String\">" + aryData[0] + "</Data></Cell>");
}
else if (aryData.Length == 2)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\"><Data ss:Type=\"" + aryData[0] + "\">" + aryData[1] + "</Data></Cell>");
}
else if (aryData.Length == 3)
{
sw2.WriteLine(" <Cell ss:Index=\"" + (col + 1) + "\" ss:StyleID=\"s21\"><Data ss:Type=\"" + aryData[0] + "\">" + aryData[1] + "</Data></Cell>");
}
}
sw2.WriteLine(" </Row>");
}
sw2.WriteLine(" </Table>");
sw2.WriteLine("</Worksheet>");
sw2.WriteLine("</Workbook>");
//*****文件另存为*****
sw2 = null;
sw.Close();
sw.Dispose();
//********************
// RepairXls(filePathName);
#endregion
}
/// <summary>
/// 把 Xml 格式的 Excel 文件转换成 二进制格式的 Excel 文件
/// 2011-07-03 李峰
/// </summary>
/// <param name="xlsPath"></param>
//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
}
}