Files
ayanova7/source/Plugins/AyaNova.Plugin.ExportToExcel/ExportToExcel.cs
2020-05-06 19:19:16 +00:00

475 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AyaNova.PlugIn;
using GZTW.AyaNova.BLL;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Data;
using Infragistics.Documents.Excel;
namespace AyaNova.Plugin.ExportToExcel
{
class ExportToExcel : IAyaNovaPlugin
{
//Keep all the object types we want to deal with in a collection
//so that we can quickly check it when asked
private static List<RootObjectTypes> ObjectsWeCanDealWith = null;
private static bool bLicensed = false;
//Holds the image resources from AyaNova
//so we can display the correct icons in our plugin
System.Resources.ResourceManager resman = null;
//Holds the current logged in user's localized text
//lookup object
LocalizedTextTable LocaleText = null;
#region IAyaNovaPlugin Members
#region interface properties
public string PluginName
{
get { return "ExportToXls"; }
}
public string PluginVersion
{
get { return "7.6"; }
}
public string About
{
get
{
return "AyaNova ExportToXls plugin";
}
}
public Guid PluginID
{
get { return new Guid("{76912765-A95F-49ce-B1B1-1A6CECD1497D}"); }
}
public System.Drawing.Image PluginSmallIcon
{
get { return Resource.ExportToExcel16; }
}
public System.Drawing.Image PluginLargeIcon
{
get { return Resource.ExportToExcel32; }
}
public System.Resources.ResourceManager AyaNovaResourceManager
{
set { resman = value; }
get { return resman; }
}
#endregion interface properties
#region Initialization and Close
public bool Initialize(Version AyaNovaVersion, LocalizedTextTable localizedText)
{
LocaleText = localizedText;
//bLicensed = (!string.IsNullOrEmpty(AyaBizUtils.PluginLicensedVersion("ExportToXls")));
//case 2094
bLicensed = AyaBizUtils.PluginAllowed("ExportToXls", AyaNova.PlugIn.ExportToExcel.Timestamp.BuildAt);
if (!bLicensed && AyaBizUtils.PluginTooNew("ExportToXls", AyaNova.PlugIn.ExportToExcel.Timestamp.BuildAt))
{
MessageBox.Show(
"NOT LICENSED!\r\n\r\nThis ExportToXls plugin was built " +
AyaNova.PlugIn.ExportToExcel.Timestamp.BuildAt.ToString() + "\r\n" +
"but your license subscription for it ended " +
AyaBizUtils.PluginSubscriptionDate("ExportToXls").ToString() + "\r\n" +
"\r\nDowngrade back to your previous version or purchase a subscription to use this plugin.");
return false;
}
if (AyaNovaVersion.Major < 7)
{
MessageBox.Show("This ExportToXls plugin requires AyaNova version 7 or newer");
return false;
}
ObjectsWeCanDealWith = new List<RootObjectTypes>();
//ObjectsWeCanDealWith.Add(RootObjectTypes.Nothing);
ObjectsWeCanDealWith.Add(RootObjectTypes.User);
ObjectsWeCanDealWith.Add(RootObjectTypes.Schedule);
ObjectsWeCanDealWith.Add(RootObjectTypes.WorkorderService);
ObjectsWeCanDealWith.Add(RootObjectTypes.Workorder);
ObjectsWeCanDealWith.Add(RootObjectTypes.Memo);
return true;
}
public void Close()
{
;
}
#endregion Initialization and close
#region ShowMenu?
public bool SingleObjectMenuShow(RootObjectTypes objectType)
{
return false;
// return (ObjectsWeCanDealWith.Contains(objectType));
}
public bool MultipleObjectsMenuShow(RootObjectTypes objectType)
{
if (objectType != RootObjectTypes.Schedule) return true;
return false;
//return (ObjectsWeCanDealWith.Contains(objectType));
}
#endregion show menu?
#region Menu options
public List<AyaNovaPluginMenuItem> SingleObjectMenuOptions(RootObjectTypes objectType, object ayaNovaObject)
{
return null;//stick to lists for now
//if (!ObjectsWeCanDealWith.Contains(objectType)) return null;
//if (!PFC()) return null;
//List<AyaNovaPluginMenuItem> list = new List<AyaNovaPluginMenuItem>();
//switch (objectType)
//{
// default:
// //If it's not nothing and we already know it's something we can deal with
// //then present the export
// list.Add(new AyaNovaPluginMenuItem("EXCELEXPORT_SELECTED", "Export to Excel", null, null));
// break;
//}
//return list;
}
public List<AyaNovaPluginMenuItem> MultipleObjectsMenuOptions(RootObjectTypes objectType)
{
//if (!ObjectsWeCanDealWith.Contains(objectType)) return null;
if (!PFC()) return null;
if (objectType == RootObjectTypes.Nothing) return null;
List<AyaNovaPluginMenuItem> list = new List<AyaNovaPluginMenuItem>();
list.Add(new AyaNovaPluginMenuItem("XlsEXPORT_SELECTED", "Export to Xls", null, null));
return list;
}
/// <summary>
/// pre flight check
/// </summary>
/// <returns></returns>
private bool PFC()
{
if (!bLicensed)
{
ShowWarning("An AyaNova ExportToXls plugin license is required to use this plugin.", "Not licensed");
return false;
}
return true;
}
private void ShowWarning(string swarn, string scaption)
{
MessageBox.Show(swarn, scaption, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
#endregion
#region Menu Commands
#region LIST OBJECT COMMAND
/// <summary>
/// LIST OBJECT
/// </summary>
/// <param name="commandKey"></param>
/// <param name="objectType"></param>
/// <param name="objectIDList"></param>
/// <param name="listObject"></param>
/// <returns></returns>
public bool CommandSelectedForList(string commandKey, RootObjectTypes objectType, List<Guid> objectIDList, object listObject)
{
//bool bAllItemsInGridWereSelected = objectIDList.Count == 0;
//if (commandKey == "EXCELEXPORT_SELECTED")
//{
// switch (objectType)
// {
// case RootObjectTypes.Schedule:
// {
// }
// break;
// case RootObjectTypes.User:
// {
// }
// break;
// case RootObjectTypes.WorkorderService:
// {
// }
// break;
// case RootObjectTypes.Memo:
// {
// }
// break;
// default:
// return false;
// }
//}
DoExport(listObject, objectIDList);
return false;
}
#endregion list object command
#region SINGLE OBJECT COMMAND
/// <summary>
/// SINGLE OBJECT
/// </summary>
/// <param name="commandKey"></param>
/// <param name="objectType"></param>
/// <param name="ayaNovaObject"></param>
public void CommandSelectedForSingleObject(string commandKey, RootObjectTypes objectType, object ayaNovaObject)
{
switch (commandKey)
{
case "XlsEXPORT_SELECTED":
{
switch (objectType)
{
case RootObjectTypes.User:
{
}
break;
case RootObjectTypes.Nothing:
{
}
break;
case RootObjectTypes.Workorder:
{
}
break;
case RootObjectTypes.Memo:
{
}
break;
default:
return;
}
}
break;
}
}
#endregion single object command
#endregion menu commands
#region DoExport
/// <summary>
/// Export to xls
/// </summary>
/// <param name="listObject"></param>
/// <param name="SelectedItems"></param>
private void DoExport(object listObject, List<Guid> SelectedItems)
{
#region Convert object to dataset
DataSet listData = new DataSet();
ExportDatasetAdapter edsa = new ExportDatasetAdapter();
object o = AyaBizUtils.GetBizObjectStaticPropertyValue(listObject, "IDField");
if (o == null)
{
System.Diagnostics.Debugger.Break();//Unexpected, all lists should have one
o = "Unknown";
}
edsa.Fill(listData, listObject, SelectedItems, LocaleText, o.ToString());//every list object should have an IDField
#endregion convert object to dataset
#region Fixup names of columns
foreach (DataTable dt in listData.Tables)
foreach (DataColumn dc in dt.Columns)
{
string skey = dc.ColumnName;
skey = skey.Replace("LT_", "");
skey = skey.Replace("O_", "");
skey = skey.Replace("_Label_", "");
dc.ColumnName = skey;
}
#endregion fixup names
#region Selected rows only?
//if(SelectedItems.Count>0)
//{
// bool bSomethingToExport = false;
// //remove any row that doesn't contain any guids that are in the list of selected ids
// foreach (DataTable dt in listData.Tables)
// {
// int stopat = dt.Rows.Count;
// int nColumns=dt.Columns.Count;
// for(int x = 0; x < stopat; x++)
// {
// bool bRowMatchedSelected = false;
// DataRow dr = dt.Rows[x];
// for (int y = 0; y < nColumns; y++)
// {
// string sGuid = AyaBizUtils.rxGuid.Match(dr[y].ToString()).Value;
// if(!string.IsNullOrEmpty(sGuid))
// {
// Guid gid=new Guid(sGuid);
// if (SelectedItems.Contains(gid))
// {
// bRowMatchedSelected = true;
// break;
// }
// }
// }//end columns loop
// if (!bRowMatchedSelected)
// {
// //remove the row, decrement stopat
// dt.Rows.RemoveAt(x);
// stopat--;
// x--;
// }
// }//end rows loop
// if(dt.Rows.Count>0) bSomethingToExport=true;
// }//end tables loop
// if (!bSomethingToExport)
// {
// MessageBox.Show("Nothing to export");
// return;
// }
//}
#endregion selected rows only
//drop the listidfield column
foreach (DataTable dt in listData.Tables)
if (dt.Columns.Contains("LISTIDFIELD")) dt.Columns.Remove("LISTIDFIELD");
#region Export to Excel workbook
Workbook workbook = new Infragistics.Documents.Excel.Workbook();
workbook.SetCurrentFormat(WorkbookFormat.Excel97To2003);
foreach (DataTable table in listData.Tables)
{
// Create the worksheet to represent this data table
string swsName = table.TableName;
if (swsName.Length > 31) swsName = swsName.Substring(0, 31);//worksheet name must be no more than 31 chs
Infragistics.Documents.Excel.Worksheet worksheet = workbook.Worksheets.Add(swsName);
// Create column headers for each column
for (int columnIndex = 0; columnIndex < table.Columns.Count; columnIndex++)
{
worksheet.Rows[0].Cells[columnIndex].Value = table.Columns[columnIndex].ColumnName;
}
// Starting at row index 1, copy all data rows in
// the data table to the worksheet
int rowIndex = 1;
foreach (DataRow dataRow in table.Rows)
{
Infragistics.Documents.Excel.WorksheetRow row = worksheet.Rows[rowIndex++];
for (int columnIndex = 0; columnIndex < dataRow.ItemArray.Length; columnIndex++)
{
//case 1698
object ob = dataRow.ItemArray[columnIndex];
if (ob is byte[])
row.Cells[columnIndex].Value = "BINARY DATA";
else if (ob is CSLA.SmartDate)
{
row.Cells[columnIndex].Value = ((CSLA.SmartDate)ob).Date;
}
else
row.Cells[columnIndex].Value = ob;
}
}
}
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
//save the file to location of users choosing
string sFileName = LocaleText.GetLocalizedText(GetBizObjectLocaleKey(listObject));
if (string.IsNullOrEmpty(sFileName))
sFileName = listObject.ToString().Replace("GZTW.AyaNova.BLL.", "");
saveFileDialog1.FileName = sFileName + ".xls";
if (saveFileDialog1.ShowDialog() == DialogResult.Cancel) return;
if (!string.IsNullOrEmpty(saveFileDialog1.FileName))
{
workbook.Save(saveFileDialog1.FileName);
}
#endregion Export to excel
}
/// <summary>
/// Use reflection to find the locale key for a biz object
/// (if the biz object doesn't have a LocaleKey property it returns
/// the name of the biz object instead)
/// </summary>
/// <param name="BizObject"></param>
public static string GetBizObjectLocaleKey(object BizObject)
{
try
{
PropertyInfo pi = BizObject.GetType().GetProperty("LocaleKey");
if (pi == null) return BizObject.ToString();
object propvalue = pi.GetValue(BizObject, null).ToString();
if (propvalue != null)
return propvalue.ToString();
else
return BizObject.ToString();
}
catch
{
//log.Error("GetBizObjectLocaleKey", e);
//MessageBox.Show("GetBizObjectLocaleKey:" + e.Message);
return "";
}
//return BizObject.ToString();
}
#endregion do export
#endregion
}
}