反射导出excel案例

系统 1395 0

1.代码案例:

protected void btnExportExcel_Click(object sender, EventArgs e)
{
SetSearchValue();
Dictionary<string, string> titles = new Dictionary<string, string>();
titles.Add("ProductName", "产品名称");

titles.Add("DanOrShuang", "单机/双机");

titles.Add("SN1", "SN1");

titles.Add("SN2", "SN2");

titles.Add("InstallQTY", " 安装数量");

titles.Add("ServerName", "服务器名称");

titles.Add("Area", " 区域");

titles.Add("CustomerName", " 客户名称");

titles.Add("CinemaName", "影院名称");

SII_ExcelExportUtil.ExcelExport("Tenancy Equipment", "Tenancy Equipment", this, _bll.ExportExcel(_search), titles);
}

======================
public class SII_ExcelExportUtil:ExcelExportUtil
{
public static void ExcelExport(string FileName, string sheetName, Page page, IList list,Dictionary<string,string> Titles)
{

new SII_ExcelExportUtil().ExcelExportData(FileName, sheetName, page, list, Titles);
}
}

2.

----------------

public virtual bool ExcelExportData(string FileName, string sheetName, Page page, IList list, Dictionary<string, string> Titles)
{
Excel.Application m_xlApp = null;
string path;
string floder;
GetPath(FileName, page, out path, out floder);
bool result = false;
//if (list != null && list.Count > 0)
if (Titles!=null && Titles.Count>0)
{

int rowNum = list.Count;//行数
int columnNum = Titles.Count;//列数
m_xlApp = new Excel.Application();
m_xlApp.DisplayAlerts = false;//不显示更改提示
m_xlApp.Visible = false;

Excel.Workbooks workbooks = m_xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1

try
{
worksheet.Name = sheetName;
string[,] datas = new string[rowNum + 1, columnNum];

int columnIndex = 0;
foreach (KeyValuePair<string, string> item in Titles)
{
datas[0, columnIndex] = item.Value;
columnIndex++;
}
Excel.Range range = SetColumsStyle(columnNum, worksheet);

if (list != null && list.Count > 0)
{
PropertyInfo[] Columns = list[0].GetType().GetProperties();
for (int index = 0; index < list.Count; index++)
{

//在当前行中,逐列获得数据
int cIndex = 0;
foreach (KeyValuePair<string, string> item in Titles)
{
PropertyInfo pro = Columns.First(p => p.Name == item.Key);
string value = "";
if (pro != null)
{
object resultValue = pro.GetValue(list[index], null);
if (resultValue is DateTime)
{
value = Convert.ToDateTime(resultValue).ToString("yyyy/MM/dd");
}
else value = resultValue == null ? "" : resultValue.ToString();
}
datas[index + 1, cIndex] = value;
cIndex++;
}
}
}
range = SetRowData(m_xlApp, path, rowNum, columnNum, worksheet, datas, range);
workbook.SaveAs(path);
result = true;
}
catch (Exception ex)
{
}
finally
{
EndReport(m_xlApp);
}
HttpDown(path, page, floder);
}
return result;
}

-----------

private Excel.Range SetRowData(Excel.Application m_xlApp, string path, int rowNum, int columnNum, Excel.Worksheet worksheet, string[,] datas, Excel.Range range)
{
Excel.Range fchR = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]];
fchR.Value2 = datas;

worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
m_xlApp.WindowState = Excel.XlWindowState.xlMinimized;

range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]];
//range.Interior.ColorIndex = 15;//15代表灰色
range.Font.Size = 9;
range.RowHeight = 14.25;
//range.Borders.LineStyle = 1;
range.HorizontalAlignment = 1;
ActiveWindow(m_xlApp);
//workbook.SaveCopyAs(path);

return range;
}

反射导出excel案例


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论