如何将excel表格导入金山小程序

如何将excel表格导入金山小程序,第1张

做过一个项目中EXCEL导入是纯JAVA程序写的,但是比较繁琐。自定义了一个XML文件,通过XML文件配置导入数据字段的约束和关系。例如,主键、外键、是否允许为空等约束,通过此列对应数据库某个表中相应的主键,及联合主键,各列之间的关系等都考虑在内。通过JDOM解析XML文件,迭代后取相当的值判断,生成导入错误文件。正确时保存入数据库,返回前台信息。

看到很多关于使用MicrosoftJetOLEDB40。首先这是一个非常常见的、很简单有效的和Excel进行数据交流的方式。另外补充一下这种方式有个缺点,就是如果Excel的Sheet名不规范,例如有空格、连字符(-)和过长的时候,这个方法是不能够正确获取数据的。这里提供一个用COM+来访问Excel的方法。COM+访问比较复杂,要求客户端上必须安装Excel。除非你经常要处理Sheet名不规范的Excel。否则我还是建议你使用OLEDB方法。using System;

using SystemData;

using Excel;

using SystemIO;

namespace TestExcelCom

{

/// <summary>

/// 将DataView中的数据导入Excel文件中

/// 作者:Rexsp

/// 创建:2004-4-4

/// </summary>

public class OutputExcel

{

#region 私有成员

/// <summary>

/// 数据的DataView

/// </summary>

private DataView dv=null;

/// <summary>

/// 表格标题

/// </summary>

private string title=null;

/// <summary>

/// 输出文件路径

/// </summary>

private string outFilePath=null;

/// <summary>

/// 输入文件名

/// </summary>

private string inputFilePath=null;

#endregion #region 公共属性

/// <summary>

/// 数据的DataView

/// </summary>

public DataView DV

{

set{dv=value;}

}

/// <summary>

/// 表格标题

/// </summary>

public string Title

{

set{title=value;}

get{return title;}

}

/// <summary>

/// 输出文件路径

/// </summary>

public string OutFilePath

{

set{outFilePath=value;}

get{return outFilePath;}

}

/// <summary>

/// 输入文件路径

/// </summary>

public string InputFilePath

{

set{inputFilePath=value;}

get{return inputFilePath;}

}

#endregion

#region 构造函数

public OutputExcel()

{

}

public OutputExcel(DataView dv,string title)

{

//

// TODO: 在此处添加构造函数逻辑

//

}

#endregion #region 公共方法

public void CreateExcel()

{

int rowIndex=4;//行起始坐标

int colIndex=1;//列起始坐标 ApplicationClass myApp=null;

Workbook myBook=null;

Worksheet mySheet=null; //如果文件不存在,则将模板文件拷贝一份作为输出文件

//这里如果通过FileCreate来创建文件是不行的,因为xls

//的空文件也有固定的格式,跟文本不一样的,也许有其它

//通过程序直接生成excel的方法,大家可以尝试尝试的

if(!FileExists(outFilePath))

{

FileCopy(inputFilePath,outFilePath,true);

} myApp= new ApplicationClass();

myAppVisible=false;

object oMissiong=SystemReflectionMissingValue;

myAppWorkbooksOpen(outFilePath,oMissiong,oMissiong,oMissiong,oMissiong, oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);

myBook=myAppWorkbooks[1];

mySheet=(Worksheet)myBookActiveSheet;

//

//取得标题

//

foreach(DataColumn col in dvTableColumns)

{

colIndex++;

mySheetCells[4,colIndex] = colColumnName;

mySheetget_Range(mySheetCells[4,colIndex],mySheetCells[4,colIndex])HorizontalAlignment = XlVAlignxlVAlignCenter; //设置标题格式为居中对齐

} //

//取得表格中的数据

//

foreach(DataRowView row in dv)

{

rowIndex ++;

colIndex = 1;

foreach(DataColumn col in dvTableColumns)

{

colIndex ++;

if(colDataType == SystemTypeGetType("SystemDateTime"))

{

mySheetCells[rowIndex,colIndex] = (ConvertToDateTime(row[colColumnName]ToString()))ToString("yyyy-MM-dd");

mySheetget_Range(mySheetCells[rowIndex,colIndex],mySheetCells[rowIndex,colIndex])HorizontalAlignment = XlVAlignxlVAlignCenter;//设置日期型的字段格式为居中对齐

}

else

if(colDataType == SystemTypeGetType("SystemString"))

{

mySheetCells[rowIndex,colIndex] = "'"+row[colColumnName]ToString();

mySheetget_Range(mySheetCells[rowIndex,colIndex],mySheetCells[rowIndex,colIndex])HorizontalAlignment = XlVAlignxlVAlignCenter;//设置字符型的字段格式为居中对齐

}

else

{

mySheetCells[rowIndex,colIndex] = row[colColumnName]ToString();

}

}

}

//

//加载一个合计行

//

int rowSum = rowIndex + 1;

int colSum = 2;

mySheetCells[rowSum,2] = "合计";

mySheetget_Range(mySheetCells[rowSum,2],mySheetCells[rowSum,2])HorizontalAlignment = XlHAlignxlHAlignCenter;

//

//设置选中的部分的颜色

//

mySheetget_Range(mySheetCells[rowSum,colSum],mySheetCells[rowSum,colIndex])Select();

mySheetget_Range(mySheetCells[rowSum,colSum],mySheetCells[rowSum,colIndex])InteriorColorIndex = 19;//设置为浅**,共计有56种

//

//取得整个报表的标题

//

mySheetCells[2,2] = title;

//

//设置整个报表的标题格式

//

mySheetget_Range(mySheetCells[2,2],mySheetCells[2,2])FontBold = true;

mySheetget_Range(mySheetCells[2,2],mySheetCells[2,2])FontSize = 22;

//

//设置报表表格为最适应宽度

//

mySheetget_Range(mySheetCells[4,2],mySheetCells[rowSum,colIndex])Select();

mySheetget_Range(mySheetCells[4,2],mySheetCells[rowSum,colIndex])ColumnsAutoFit();

//

//设置整个报表的标题为跨列居中

//

mySheetget_Range(mySheetCells[2,2],mySheetCells[2,colIndex])Select();

mySheetget_Range(mySheetCells[2,2],mySheetCells[2,colIndex])HorizontalAlignment = XlHAlignxlHAlignCenterAcrossSelection;

//

//绘制边框

//

mySheetget_Range(mySheetCells[4,2],mySheetCells[rowSum,colIndex])BordersLineStyle = 1;

mySheetget_Range(mySheetCells[4,2],mySheetCells[rowSum,2])Borders[XlBordersIndexxlEdgeLeft]Weight = XlBorderWeightxlThick;//设置左边线加粗

mySheetget_Range(mySheetCells[4,2],mySheetCells[4,colIndex])Borders[XlBordersIndexxlEdgeTop]Weight = XlBorderWeightxlThick;//设置上边线加粗

mySheetget_Range(mySheetCells[4,colIndex],mySheetCells[rowSum,colIndex])Borders[XlBordersIndexxlEdgeRight]Weight = XlBorderWeightxlThick;//设置右边线加粗

mySheetget_Range(mySheetCells[rowSum,2],mySheetCells[rowSum,colIndex])Borders[XlBordersIndexxlEdgeBottom]Weight = XlBorderWeightxlThick;//设置下边线加粗

myBookSave();;

myBookClose( true,outFilePath,true);

SystemRuntimeInteropServicesMarshalReleaseComObject(mySheet);

SystemRuntimeInteropServicesMarshalReleaseComObject(myBook);

SystemRuntimeInteropServicesMarshalReleaseComObject(myApp);

GCCollect();

}

#endregion

}

}

以上就是关于如何将excel表格导入金山小程序全部的内容,包括:如何将excel表格导入金山小程序、如何将C语言的输出倒入Excel工作表、求助:怎么用一个Java程序实现Excel数据导入等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10063050.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存