利用NPOI和List泛型匯出Excel
具體方法不要看,這裡這是一個參考
//彈出表的儲存路徑
private void Output_Click_1(object sender,RoutedEventArgs e)//匯出方式合為一個方法體(強制轉換sender)
{
if (Grid_Center.ItemsSource != null)
{
DevExpress.Xpf.Bars.BarButtonItem s =(DevExpress.Xpf.Bars.BarButtonItem)sender;
string name = s.Name;
Microsoft.Win32.SaveFileDialogsave = new Microsoft.Win32.SaveFileDialog();//有沒有隻取路徑的控制元件?貌似沒有
ActionResult ar = newActionResult(true, "");
switch (name)
{
case"bbiOutputXls":
save.Filter ="*.xls|.xls";
save.FileName = "檔名";
save.Title ="Export To Excel";
break;
case"bbiOutputXls_1":
save.Filter ="*.xls|.xls";
save.FileName = "檔名";
save.Title ="Export To Excel";
break;
case "bbiOutputCsv":
save.Filter ="*.csv|.csv";
save.FileName = "檔名";
save.Title ="Export To Csv";
break;
default:
break;
}
bool? result =save.ShowDialog();
if (result.Value)//點選儲存
{
string path =save.FileName;
try
{
switch (name)
{
case"bbiOutputXls":
//DevExpress.XtraPrinting.XlsExportOptions options = newDevExpress.XtraPrinting.XlsExportOptions();
//GridView_Center.ExportToXls(path,options);
ar=OutToExcel(path,1);
break;
case"bbiOutputXls_1"://匯出表
ar=OutToExcel(path,2);
break;
case"bbiOutputCsv":
GridView_Center.ExportToCsv(path);
break;
default:
break;
}
if (ar.IsSucceeded)
{
TopMessageBox.Show(TopDialogType.Tip, MessageBoxString.匯出成功);
}
else
{
TopMessageBox.Show(TopDialogType.Tip, MessageBoxString.匯出失敗+ar.Message);
}
}
catch
{
TopMessageBox.Show(TopDialogType.Wrong,MessageBoxString.匯出失敗+ar.Message);
}
}
}
else
{
TopMessageBox.Show(TopDialogType.Warning, MessageBoxString.無資料);
}
}
//利用NOPI把資料寫入記憶體,List泛型集合
public override ActionResultOutToExcel(string path, int flag)
{
List<TView> viewList = Grid_Center.ItemsSource asList<TView>;
if (viewList != null && viewList.Count > 0)
{
List<TEntity> entityList= new List<TEntity>();
Agent agent = new Agent();
ActionResult ar;
if (flag == 2) //如果匯出實體,則從檢視轉成實體
{
var q=viewList[0];
for (int i = 0; i <viewList.Count(); i++)
{
TEntity newEntity = newTEntity();
q = viewList[i];
ar = agent.GetByView(LoginInfo.CurrentUser.員工編號, q, outnewEntity);
if (ar.IsSucceeded)
{
entityList.Add(newEntity);
}
}
}
List<TableFiled> tf =agent.GetTableFiled(flag) ;
HSSFWorkbook workBook = newHSSFWorkbook();
HSSFSheet sheet =(HSSFSheet)workBook.CreateSheet("表單");
//建立列頭
IRow head = sheet.CreateRow(0);
for (int lie = 0; lie <tf.Count; lie++)
{
ICell cellhead =head.CreateCell(lie);
cellhead.SetCellValue(tf[lie].欄位名稱.ToString());
}
int iRowIndex = 1;
int iCellIndex = 0;
StringBuilder Value = newStringBuilder();
double Valueint;
DateTime ValuedateTime;
//匯出檢視列表
if (flag == 1)
{
//行
for (int i = 0; i <viewList.Count; i++)
{
IRow DataRow =sheet.CreateRow(iRowIndex);
//列
for (int j = 0; j <tf.Count; j++)
{
//Rows[i].CreateCell(j).SetCellValue(strList[i]);
ICell cell =DataRow.CreateCell(iCellIndex);
if(viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null) == null)
{
Value.Append("");
cell.SetCellValue(Value.ToString());
}
else
{
object oo =viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null).GetType();
if(oo.ToString()=="System.Decimal"||oo.ToString()=="System.Int16")
{
Valueint=Convert.ToDouble( viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null));
cell.SetCellValue(Valueint);
}
else if(oo.ToString() == "System.DateTime")
{
ValuedateTime =(DateTime)viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null);
cell.SetCellValue(ValuedateTime.ToShortDateString());
}
else
{
Value.Append(viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null).ToString());
cell.SetCellValue(Value.ToString());
}
}
Value.Length = 0;
Valueint = 0;
//ValuedateTime=0
iCellIndex++;
}
iCellIndex = 0;
iRowIndex++;
}
}
//匯出主表
if (flag == 2)
{
//行
for (int i = 0; i <entityList.Count; i++)
{
IRow DataRow =sheet.CreateRow(iRowIndex);
//列
for (int j = 0; j< tf.Count; j++)
{
ICell cell =DataRow.CreateCell(iCellIndex);
if(viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null) == null)
{
Value.Append("");
cell.SetCellValue(Value.ToString());
}
else
{
object oo =viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null).GetType();
if(oo.ToString() == "System.Decimal" || oo.ToString() == "System.Int16")
{
Valueint =Convert.ToDouble(viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null));
cell.SetCellValue(Valueint);
}
else if(oo.ToString() == "System.DateTime")
{
ValuedateTime = (DateTime)viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null);
cell.SetCellValue(ValuedateTime.ToShortDateString());
}
else
{
Value.Append(viewList[i].GetType().GetProperty(tf[j].欄位名稱).GetValue(viewList[i],null).ToString());
cell.SetCellValue(Value.ToString());
}
}
Value.Length = 0;
Valueint = 0;
//ValuedateTime=0
iCellIndex++;
}
iCellIndex = 0;
iRowIndex++;
}
}
MemoryStream ms = newMemoryStream();
workBook.Write(ms);
ms.Flush();
ms.Position = 0;
using (FileStream fs = newFileStream(path, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0,data.Length);
fs.Flush();
}
#endregion
try
{
return newActionResult(true, "");
}
catch (Exception ex)
{
return newActionResult(false, ex.Message);
}
#endregion
}
else
{
return new ActionResult(false,"沒有可匯出的內容!");
}
}