Excel的操作
此篇讲点AX下的excel操作,打开,写入,复制,插入,读取,显示。excel保存为模板,然后在AX通过SysExcelApplication SysExcelWorkbooks SysExcelWorkbook SysExcelWorksheets SysExcelWorksheet SysExcelCells SysExcelCell 等对象打开模板对单元格进行值的填充,最后以临时打开的方式显示给用户。
用户是保存到哪里,还是直接关闭,都交给用户去决定。
这次就不上图了,以代码为主。
View Code
public class ReportRun extends ObjectRun
{
//Excel操作的相关对象,层级结构,逐层深入
SysExcelApplication excel;
SysExcelWorkbooks books;
SysExcelWorkbook book;
SysExcelWorksheets sheets;
SysExcelWorksheet sheet;
SysExcelCells cells;
SysExcelCell cell;
SysExcelRange column;
COM range;
}
{
//Excel操作的相关对象,层级结构,逐层深入
SysExcelApplication excel;
SysExcelWorkbooks books;
SysExcelWorkbook book;
SysExcelWorksheets sheets;
SysExcelWorksheet sheet;
SysExcelCells cells;
SysExcelCell cell;
SysExcelRange column;
COM range;
}
先是整个excel,往下走是 工作表 books (就是excel的哪一页),确定 单页工作表 book 后,在来是 片 sheets ....(省略)就这样下去到单元格 cell。
还是代码直观
View Code
private void excelImport()
{
FilenameOpen m_file;
VendParameters m_parameter;
;
//excel文件地址
select firstonly m_parameter;
m_file = m_parameter.PurchasePrintModel;
//对象获取
excel = SysExcelApplication::construct();
//模板
books = excel.workbooks();
//是否正常打开
if(!books.open(m_file))
return;
//是否有内容
if(!books.count())
{
info("no content!");
return;
}
//工作表的第一项
book = books.item(1);
//工作表片区
sheets = book.worksheets();
sheet = sheets.itemFromNum(1);
cells = sheet.cells();
}
{
FilenameOpen m_file;
VendParameters m_parameter;
;
//excel文件地址
select firstonly m_parameter;
m_file = m_parameter.PurchasePrintModel;
//对象获取
excel = SysExcelApplication::construct();
//模板
books = excel.workbooks();
//是否正常打开
if(!books.open(m_file))
return;
//是否有内容
if(!books.count())
{
info("no content!");
return;
}
//工作表的第一项
book = books.item(1);
//工作表片区
sheets = book.worksheets();
sheet = sheets.itemFromNum(1);
cells = sheet.cells();
}
读取单元格内容的方法
View Code
Container read(SysExcelCell sysExcelCell)
{
//excel内容读取方法
container line;
int intvalue;
real realvalue;
;
switch (sysExcelCell.value().variantType())
{
case COMVariantType::VT_EMPTY:
line += 0;
break;
case COMVariantType::VT_I1:
line += sysExcelCell.value().char();
break;
case COMVariantType::VT_I2:
line += sysExcelCell.value().short();
break;
case COMVariantType::VT_I4:
intValue = sysExcelCell.value().int();
if (intValue == 0)
{
intValue = sysExcelCell.value().long();
}
line += intValue;
break;
case COMVariantType::VT_UI1:
line += sysExcelCell.value().byte();
break;
case COMVariantType::VT_UI2:
line += sysExcelCell.value().uShort();
break;
case COMVariantType::VT_UI4:
intValue = sysExceLCell.value().uInt();
if (intValue == 0)
{
intValue = sysExcelCell.value().uLong();
}
line += intValue;
break;
case COMVariantType::VT_R4 :
realValue = sysExcelCell.value().float();
line += realValue;
break;
case COMVariantType::VT_R8 :
realValue = sysExcelCell.value().double();
line += realValue;
break;
case COMVariantType::VT_DECIMAL :
realValue = sysExcelCell.value().decimal();
line += realValue;
break;
case COMVariantType::VT_BSTR :
line += SysExcelCell.value().bstr();
break;
default:
throw error(strfmt("@SYS26908", sysExcelCell.value().variantType()));
}
return line;
}
{
//excel内容读取方法
container line;
int intvalue;
real realvalue;
;
switch (sysExcelCell.value().variantType())
{
case COMVariantType::VT_EMPTY:
line += 0;
break;
case COMVariantType::VT_I1:
line += sysExcelCell.value().char();
break;
case COMVariantType::VT_I2:
line += sysExcelCell.value().short();
break;
case COMVariantType::VT_I4:
intValue = sysExcelCell.value().int();
if (intValue == 0)
{
intValue = sysExcelCell.value().long();
}
line += intValue;
break;
case COMVariantType::VT_UI1:
line += sysExcelCell.value().byte();
break;
case COMVariantType::VT_UI2:
line += sysExcelCell.value().uShort();
break;
case COMVariantType::VT_UI4:
intValue = sysExceLCell.value().uInt();
if (intValue == 0)
{
intValue = sysExcelCell.value().uLong();
}
line += intValue;
break;
case COMVariantType::VT_R4 :
realValue = sysExcelCell.value().float();
line += realValue;
break;
case COMVariantType::VT_R8 :
realValue = sysExcelCell.value().double();
line += realValue;
break;
case COMVariantType::VT_DECIMAL :
realValue = sysExcelCell.value().decimal();
line += realValue;
break;
case COMVariantType::VT_BSTR :
line += SysExcelCell.value().bstr();
break;
default:
throw error(strfmt("@SYS26908", sysExcelCell.value().variantType()));
}
return line;
}
excel的写入
View Code
private void excelWrite()
{
container m_agreement;
;
rowNum = 23;
//片区范围,复制和插入
range = sheet.range(strfmt("A%1:L%1",rowNum-1)).comObject();
row = range.EntireRow();
row.copy();
row.insert();
//单元格值的写入
cell = cells.item(rowNum,1);
cell.value("单元格内容");
//金额的统计转换为中文大写
cell = cells.item(rowNum,2);
cell.value("合计人民币金额(大写):" + global::numeralsToTxt_CN(pricesCount,false,true,10));
//通过容器读取单元格内容
cell = cells.item(rowNum+1,7);
m_agreement = this.read(cell);
//读取容器内容写入单元格
cell.value(strfmt(conpeek(m_agreement,1),PaymTerm::find(purchTable.Payment).Description));
//显示excel
excel.visible(true);
}
{
container m_agreement;
;
rowNum = 23;
//片区范围,复制和插入
range = sheet.range(strfmt("A%1:L%1",rowNum-1)).comObject();
row = range.EntireRow();
row.copy();
row.insert();
//单元格值的写入
cell = cells.item(rowNum,1);
cell.value("单元格内容");
//金额的统计转换为中文大写
cell = cells.item(rowNum,2);
cell.value("合计人民币金额(大写):" + global::numeralsToTxt_CN(pricesCount,false,true,10));
//通过容器读取单元格内容
cell = cells.item(rowNum+1,7);
m_agreement = this.read(cell);
//读取容器内容写入单元格
cell.value(strfmt(conpeek(m_agreement,1),PaymTerm::find(purchTable.Payment).Description));
//显示excel
excel.visible(true);
}