要是服务器上没有装着excel 可以用c#导出excel表吗
可以使用二进制读写Excel的开源组件orgin2bitsMyXlsdll导出真正的Excel文件。
以下是我简单封装的一个ExcelHelper
using System;
using SystemData;
using SystemConfiguration;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIHtmlControls;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemCollectionsGeneric;
using orgin2bitsMyXls;
using orgin2bitsMyXlsByteUtil;
/// <summary>
///ExcelHelper 的摘要说明
/// </summary>
public class ExcelHelper
{
protected ExcelHelper()
{
}
public class TableStruct
{
private string _TableName;
private string _TableCaption;
private List<ColumnStruct> _ColumnInfoAry;
public string TableName
{
get
{
if (stringIsNullOrEmpty(_TableName))
{
return stringEmpty;
}
return _TableName;
}
set
{
_TableName = value;
}
}
public string TableCaption
{
get
{
if (stringIsNullOrEmpty(_TableCaption))
{
return TableName;
}
return _TableCaption;
}
set
{
_TableCaption = value;
}
}
public List<ColumnStruct> ColumnInfoAry
{
get
{
if (_ColumnInfoAry == null)
{
_ColumnInfoAry = new List<ColumnStruct>();
}
return _ColumnInfoAry;
}
}
public void AddColumnInfo(ColumnStruct ColumnInfo)
{
if (ColumnInfo == null)
{
return;
}
if (_ColumnInfoAry == null)
{
_ColumnInfoAry = new List<ColumnStruct>();
}
foreach (ColumnStruct col in _ColumnInfoAry)
{
if (colColumnNameEquals(ColumnInfoColumnName, StringComparisonOrdinalIgnoreCase))
{
return;
}
}
_ColumnInfoAryAdd(ColumnInfo);
}
public ColumnStruct GetColumnInfo(string ColumnName)
{
if (stringIsNullOrEmpty(ColumnName) )
{
return null;
}
if (_ColumnInfoAry == null)
{
return null;
}
ColumnStruct ColumnInfo = null;
foreach (ColumnStruct col in _ColumnInfoAry)
{
if (colColumnNameEquals(ColumnName, StringComparisonOrdinalIgnoreCase))
{
ColumnInfo = col;
}
}
return ColumnInfo;
}
}
public class ColumnStruct
{
private string _ColumnName;
private string _ColumnCaption;
private string _ColumnTextFormat;
public string ColumnName
{
get
{
if (stringIsNullOrEmpty(_ColumnName))
{
return stringEmpty;
}
return _ColumnName;
}
set
{
_ColumnName = value;
}
}
public string ColumnCaption
{
get
{
if (stringIsNullOrEmpty(_ColumnCaption))
{
return ColumnName;
}
return _ColumnCaption;
}
set
{
_ColumnCaption = value;
}
}
public string ColumnTextFormat
{
get
{
if (stringIsNullOrEmpty(_ColumnTextFormat))
{
return stringEmpty;
}
return _ColumnTextFormat;
}
set
{
_ColumnTextFormat = value;
}
}
}
public static void ExportToExcel(DataSet ds,List<TableStruct> TableInfoAry)
{
if (ds == null)
{
ds = new DataSet();
}
if ( TableInfoAry == null )
{
TableInfoAry = new List<TableStruct>();
}
XlsDocument xls = new XlsDocument();
xlsFileName = DateTimeNowToString("yyyyMMddHHmmssffff",SystemGlobalizationDateTimeFormatInfoInvariantInfo);
xlsSummaryInformationAuthor = "wangmh"; //填加xls文件作者信息
xlsSummaryInformationNameOfCreatingApplication = "Microsoft Excel"; //填加xls文件创建程序信息
xlsSummaryInformationLastSavedBy = "wangmh"; //填加xls文件最后保存者信息
xlsSummaryInformationComments = "Gwm"; //填加xls文件作者信息
xlsSummaryInformationTitle = "Gwm"; //填加xls文件标题信息
xlsSummaryInformationSubject = "Gwm";//填加文件主题信息
xlsDocumentSummaryInformationCompany = "Gwm";//填加文件公司信息
foreach (TableStruct TableInfo in TableInfoAry)
{
DataTable dt = dsTables[TableInfoTableName];
if (dt == null)
{
continue;
}
Worksheet sheet = xlsWorkbookWorksheetsAdd(TableInfoTableCaption);
//设置标头栏
ushort ColumnIndex = 1;
foreach (ColumnStruct ColStruct in TableInfoColumnInfoAry)
{
ushort RowIndex = 1;
Row row = sheetRowsAddRow(RowIndex);
if (!dtColumnsContains(ColStructColumnName))
{
continue;
}
Cell cell = null;
if (rowCellExists(ColumnIndex))
{
cell = rowGetCell(ColumnIndex);
}
else
{
cell = sheetCellsAdd(RowIndex, ColumnIndex, null);
//rowAddCell(cell);
}
cellValue = ColStructColumnCaption;
cellFontWeight = FontWeightBold;
cellHorizontalAlignment = HorizontalAlignmentsCentered;
cellBottomLineStyle = 2;
cellBottomLineColor = ColorsGrey;
cellFontHeight = 10 20;
cellVerticalAlignment = VerticalAlignmentsCentered;
ushort ColumnMaxLength = GetColumnValueMaxLength(dt, ColStruct);
//设定列宽为自适应宽度
ColumnInfo colInfo = new ColumnInfo(xls, sheet);//生成列格式对象
//设定colInfo格式的起作用的列为第1列到第5列(列格式为0-base)
colInfoColumnIndexStart = (ushort)(ColumnIndex-1);
colInfoColumnIndexEnd = colInfoColumnIndexStart;
colInfoWidth = (ushort)(ColumnMaxLength 256);//列的宽度计量单位为 1/256 字符宽
sheetAddColumnInfo(colInfo);//把格式附加到sheet页上(注:AddColumnInfo方法有点小问题,不能把colInfo对象多次附给sheet页)
ColumnIndex++;
}
for (ushort i = 0; i < dtRowsCount; i++)
{
ushort RowIndex = (ushort)(i + 2);
Row row = sheetRowsAddRow(RowIndex);
int j = 0;
foreach (ColumnStruct ColStruct in TableInfoColumnInfoAry)
{
if ( !dtColumnsContains(ColStructColumnName) )
{
continue;
}
ColumnIndex = (ushort)(j + 1);
Cell cell = null;
if (rowCellExists(ColumnIndex))
{
cell = rowGetCell(ColumnIndex);
}
else
{
cell = sheetCellsAdd(RowIndex, ColumnIndex, null);
//rowAddCell(cell);
}
object objValue = dtRows[i][ColStructColumnName];
cellValue = GetColumnValueFormat(dt,ColStruct,objValue);
cellFontWeight = FontWeightNormal;
cellHorizontalAlignment = HorizontalAlignmentsCentered;
j++;
}
}
}
xlsSend();
}
private static string GetColumnValueFormat(DataTable dt, ColumnStruct ColStruct, Object ObjValue)
{
string ColumnValue = stringEmpty;
if ( ObjValue != null && ObjValue!= DBNullValue )
{
string ColumnDataType = dtColumns[ColStructColumnName]DataTypeToString();
switch (ColumnDataType)
{
case "SystemBoolean":
case "SystemByte":
{
ColumnValue = ObjValueToString();
break;
}
case "SystemDecimal":
{
if (stringIsNullOrEmpty(ColStructColumnTextFormat))
{
ColumnValue = ConvertToDecimal(ObjValue)ToString();
}
else
{
ColumnValue = ConvertToDecimal(ObjValue)ToString(ColStructColumnTextFormat);
}
break;
}
case "SystemDouble":
{
if (stringIsNullOrEmpty(ColStructColumnTextFormat))
{
ColumnValue = ConvertToDouble(ObjValue)ToString();
}
else
{
ColumnValue = ConvertToDouble(ObjValue)ToString(ColStructColumnTextFormat);
}
break;
}
case "SystemInt64":
{
if (stringIsNullOrEmpty(ColStructColumnTextFormat))
{
ColumnValue = ConvertToInt64(ObjValue)ToString();
}
else
{
ColumnValue = ConvertToInt64(ObjValue)ToString(ColStructColumnTextFormat);
}
break;
}
case "SystemInt16":
{
if (stringIsNullOrEmpty(ColStructColumnTextFormat))
{
ColumnValue = ConvertToInt16(ObjValue)ToString();
}
else
{
ColumnValue = ConvertToInt16(ObjValue)ToString(ColStructColumnTextFormat);
}
break;
}
case "SystemInt32":
{
if (stringIsNullOrEmpty(ColStructColumnTextFormat))
{
ColumnValue = ConvertToInt32(ObjValue)ToString();
}
else
{
ColumnValue = ConvertToInt32(ObjValue)ToString(ColStructColumnTextFormat);
}
break;
}
case "SystemDateTime":
{
if (stringIsNullOrEmpty(ColStructColumnTextFormat))
{
ColumnValue = ConvertToDateTime(ObjValue)ToString();
}
else
{
ColumnValue = ConvertToDateTime(ObjValue)ToString(ColStructColumnTextFormat,SystemGlobalizationDateTimeFormatInfoInvariantInfo);
}
break;
}
default:
{
ColumnValue = ObjValueToString();
break;
}
}
}
return ColumnValueTrim();
}
private static ushort GetColumnValueMaxLength(DataTable dt, ColumnStruct ColStruct)
{
ushort InitLenth = (ushort)SystemTextEncodingDefaultGetByteCount(ColStructColumnCaption);
ushort MaxLenth = InitLenth;
foreach (DataRow Row in dtRows)
{
object ObjValue = Row[ColStructColumnName];
if (ObjValue == null || ObjValue == DBNullValue)
{
continue;
}
string ColumnValue = GetColumnValueFormat(dt,ColStruct,ObjValue);
ushort ColumnValueLenth = (ushort)SystemTextEncodingDefaultGetByteCount(ColumnValue);
MaxLenth = ColumnValueLenth > MaxLenth ColumnValueLenth : MaxLenth;
}
if (MaxLenth == InitLenth)
{
//标题栏字体较大
MaxLenth += 4;
}
else
{
//内容文本前后与边框空余出一字的间隔
MaxLenth += 2;
}
return (ushort)(MaxLenth 12);
}
}
页面调用ExcelHelper类中的静态方法ExportDataToExcel导出数据集中指定Table的制定列到Excel文件并输出文件到浏览器客户端。XmlManagerGetString是多语言实现的相关方法,可以直接修改为返回当前列标题字符串。
private void ExportDataToExcel(DSSummary_DetailsQuery_SellInfo ds)
{
SystemCollectionsGenericList<ExcelHelperTableStruct> TableInfoAry = new SystemCollectionsGenericList<ExcelHelperTableStruct>();
ExcelHelperTableStruct TableInfo = new ExcelHelperTableStruct();
TableInfoTableName = dsDS_StockTableName;
TableInfoTableCaption = XmlManagerGetString("Summary_DetailsQuery_ViewCarInfoCustormerDetail_TitleTxt");
ExcelHelperColumnStruct ColStruct = new ExcelHelperColumnStruct();
ColStructColumnName = "C_CarNo";
ColStructColumnTextFormat = stringEmpty;
ColStructColumnCaption = XmlManagerGetString("IAMS_System_CarInfo_CarNo");
TableInfoAddColumnInfo(ColStruct);
//……添加其他列信息
TableInfoAryAdd(TableInfo);
ExcelHelperExportToExcel(ds, TableInfoAry);
}
希望可以帮到你,有什么问题可以联系我。Email:wangminghu2000@163com
法一:首先禁用你服务器上的杀软,看看还会不会造成该情况?若没有,说明你杀软哪个没选项没微调好
法二:参考微软KB2434932 (它是个hotfix,看看是不是适用于你们企业实际环境)
SpreadJS由于是一款纯前端的控件,默认支持的是纯前端的导入导出。
如果要导出到服务器端需要将前端生成的blob对象通过ajax或者form表单提交的方式传递到后台,后台接收到后转存为xlsx格式的Excel文件即可。
详细可以参考下面链接的文章
网页链接
你可以在每台服务器上面放同一个脚本。
这个linux脚本加入cron,这个脚本每天定时把df -k free -m uptime 这三项的结果保存到文本文件里面,比如uptime可以保存到uptime_20140112txt,以此类推。
然后脚本把这些文件ftp到一个单独的服务器上面,比如1921681100,这个服务器可以是windows。
然后写个程序读当天或者几天的txt文件,然后输出到excel。可以用你熟悉的语言来写,应该不难。
0条评论