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;
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
