|
Thank you alot, I will consider what you have said.
|
|
|
|
|
Excellent post!
You have just been Sharapova'd.
|
|
|
|
|
I Need to call a method that is present on two different classes without istantiate two different objects one for any class and call the method from an object or other switching on a flag.
Example, in the following code i would call GetSheetAt(..)
XSSFWorkbook Xwb;
XSSFSheet Xsh;
HSSFWorkbook Hwb;
String Sheets;
switch (Type) {
case XLSType.XLS:
Hwb = new HSSFWorkbook(fs);
SheetCount = Hwb.Count;
for (int i = 0; i < SheetCount; i++)
Sheets += "\n" + Hwb.GetSheetAt(i).SheetName;
break;
case XLSType.XLSX:
Xwb = new XSSFWorkbook(fs);
SheetCount = Xwb.Count;
for (int i = 0; i < SheetCount; i++)
Sheets += "\n" + Xwb.GetSheetAt(i).SheetName;
break;
default:
break;
}
i would use something like objwb.GetSheetAt(i)
Many Thanx in Advance.
SFMBE
|
|
|
|
|
The two objects are different so you need to instantiate both so the correct code gets executed..
|
|
|
|
|
GetSheetAt is implemented in the "Workbook" class, from which both inherit; but you'd still have to instantiate the correct class.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Based on what I see here:
1. 'fs ... whatever that is ... is invariant
2. you are creating new instances of HSSF... and XSSF... in the scope of a 'switch statement just to get access to methods "in" them. That's a waste.
Think about creating one instance each of HSSF... and XSSF... outside the 'switch statement ... and, then, re-use them ... assuming those Classes are not doing some strange self-modifying voodoo.
And, you really should use StringBuilder here:
XSSFWorkbook Xwb = new XSSFWorkbook(fs);
HSSFWorkbook Hwb = new HSSFWorkbook(fs);
XSSFSheet Xsh;
StringBuilder sb = new StringBuilder();
String Sheets;
switch (Type) {
case XLSType.XLS:
for (int i = 0; i < Hwb.Count; i++)
sb.AppendLine(Hwb.GetSheetAt(i).SheetName);
break;
case XLSType.XLSX:
for (int i = 0; i < Xwb.Count; i++)
sb.AppendLine(Xwb.GetSheetAt(i).SheetName);
break;
default:
break;
}
Sheets = sb.ToString(); It should be obvious, but note that this code is not tested.
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
If both inherit from the workbook class then the switch statement is superfluous
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Merry Xmas, Eddy,
A good point; it's been a long time since I played with Excel automation.
In this case, we don't really know what the variable 'Type used in the switch-statement contains ... we do know that's a bad choice for a variable name ... is it even possible that 'Type contains a fully qualified name that would work in this context ? One can only hope the OP here is posting code that somewhat "works"
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
BillWoodruff wrote: Merry Xmas, Eddy, Thanks, same to you
BillWoodruff wrote: In this case, we don't really know what the variable 'Type used in the switch-statement contains We Googled the type-names, and we think that there is a very low probability that there exists exactly two types with those names and documentation on a major website.
Could be just a coincidence.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The "Type" variable in my piece of code is symply an enumerator that assume two values observing the extension of a filename (not present in the code posted) used in fs that is a filestream.
Sorry for unusable code but when i posted the question i was in the first approach to the problem.
Thanx to all for suggestions but in the end i choiced a simply (much simply??) way to use the switch.
In the Visual Fox Pro (the my previous language) i can call a generic method from an object that can be anytype ... there is no strong type checking... the more similar way is use the Pinvoke ... but i don't like so much .. even if in some cases i used it. Initially i tryed to use a simple object of type 'object' ... but the use of switch was necessary with many cast ... so the partial initial (reaing only), but useful, result of my class for managing xls or xlsx with NPOI is :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace XLSManagement
{
public class NPOIWrapper {
enum XLSType {XLS, XLSX}
public string FileName { get; set;}
private int _CurrentSheetIndex = 0;
public int CurrentSheetIndex { get {return _CurrentSheetIndex; } set {_CurrentSheetIndex = value; } }
public NPOIWrapper() { }
public NPOIWrapper(string cFileName) {
this.FileName = cFileName;
ReadXLS();
}
private string[] _Sheet;
public string[] Sheet {get { return _Sheet;} }
private int _SheetCount = 0;
public int SheetCount {get { return _SheetCount;} }
XLSType Type;
XSSFWorkbook Xwb;
HSSFWorkbook Hwb;
ISheet[] sh;
public bool ReadXLS(string XLSFileName = "") {
bool Ret = true;
string XLSName, Extension;
StringBuilder Sheets = new StringBuilder();
FileStream fs;
XLSName = String.IsNullOrEmpty(XLSFileName)?this.FileName:XLSFileName;
Extension = Path.GetExtension(XLSName).Substring(1).ToUpper();
switch (Extension) {
case "XLS":
Type = XLSType.XLS;
break;
case "XLSX":
Type = XLSType.XLSX;
break;
default:
Ret = false;
break;
}
if(Ret) {
fs = new FileStream(XLSName, FileMode.Open, FileAccess.Read);
if (fs==null) Ret = false;
else {
switch (Type) {
case XLSType.XLS:
try {
Hwb = new HSSFWorkbook(fs);
} catch (Exception ex){ MessageBox.Show("Lettura File XLSX: \n" + ex.Message);}
_SheetCount = Hwb.Count;
sh = new ISheet[_SheetCount];
for (int i = 0; i < _SheetCount; i++) {
sh[i] = Hwb.GetSheetAt(i);
Sheets.Append("\n" + sh[i].SheetName);
}
break;
case XLSType.XLSX:
try {
Xwb = new XSSFWorkbook(fs);
} catch (Exception ex){ MessageBox.Show("Lettura File XLSX: \n" + ex.Message);}
_SheetCount = Xwb.Count;
sh = new ISheet[_SheetCount];
for (int i = 0; i < _SheetCount; i++) {
sh[i] = Xwb.GetSheetAt(i);
Sheets.Append("\n" + sh[i].SheetName);
}
break;
default:
break;
}
this._Sheet = Sheets.ToString().Substring(1).Split('\n');
}
}
return Ret;
}
public ICell GetCell(int Row, int Col) {
ICell Ret;
Ret = GetCell(Row, Col, this.CurrentSheetIndex);
return Ret;
}
public ICell GetCell(int Row, int Col, int Sheet) {
ICell Ret;
Ret = sh[Sheet].GetRow(Row).GetCell(Col);
return Ret;
}
public String GetCellValue(int Row, int Col) {
String Ret;
Ret = GetCellValue(Row, Col, this.CurrentSheetIndex);
return Ret;
}
public string GetCellValue(int Row, int Col, int Sheet) {
string Ret;
Ret = sh[Sheet].GetRow(Row).GetCell(Col).ToString();
return Ret;
}
public IRow GetRow(int Row, int Sheet) {
IRow Ret;
Ret = sh[Sheet].GetRow(Row);
return Ret;
}
public IRow GetRow(int Row) {
IRow Ret;
Ret = GetRow(Row, CurrentSheetIndex);
return Ret;
}
}
}
|
|
|
|
|
I have no examined the NPOI classes hierarchy 8|.
Thanx
|
|
|
|
|
Is true ... my use of string in that manner (is c#) is not reccomended ... but, in my partial excuse, was my first draft and my was pointed in the use of class istantiation ... ecen if your seuggestion is an aswer to my question ... the better way is to use StringBuilders 8)
Thanx
|
|
|
|
|
Here are the elements you need; assuming you can reflect on your workbooks:
using System;
using System.Reflection;
namespace ConsoleApplication6 {
class Program {
static void Main( string[] args ) {
Run( typeof( Foo ), "butter" );
Run( typeof( Fee ), "jam" );
}
private static void Run( Type t, string s ) {
object o = Activator.CreateInstance( t );
MethodInfo mi = o.GetType().GetMethod( "Print" );
mi.Invoke( o, new object[] { s } );
}
}
class Foo {
public void Print( string s ) { Console.WriteLine( "I like bread and {0}", s ); }
}
class Fee {
public void Print( string s ) { Console.WriteLine( "I like toast and {0}", s ); }
}
}
|
|
|
|
|
Thanx
this is my second version of this incomplete but runnable piece of code:
public class NPOIWrapper {
enum XLSType {XLS, XLSX}
public string FileName { get; set;}
private int _CurrentSheetIndex = 0;
public int CurrentSheetIndex { get {return _CurrentSheetIndex; } set {_CurrentSheetIndex = value; } }
public NPOIWrapper() { }
public NPOIWrapper(string cFileName) {
this.FileName = cFileName;
ReadXLS();
}
private string[] _Sheet;
public string[] Sheet {get { return _Sheet;} }
private int _SheetCount = 0;
public int SheetCount {get { return _SheetCount;} }
XLSType xlsType;
object wb;
ISheet[] sh;
public bool ReadXLS(string XLSFileName = "") {
bool Ret = true;
string XLSName, Extension;
StringBuilder Sheets = new StringBuilder();
FileStream fs;
XLSName = String.IsNullOrEmpty(XLSFileName)?this.FileName:XLSFileName;
Extension = Path.GetExtension(XLSName).Substring(1).ToUpper();
switch (Extension) {
case "XLS":
xlsType = XLSType.XLS;
break;
case "XLSX":
xlsType = XLSType.XLSX;
break;
default:
Ret = false;
break;
}
if(Ret) {
fs = new FileStream(XLSName, FileMode.Open, FileAccess.Read);
if (fs==null) Ret = false;
else {
Type t = xlsType==XLSType.XLS?typeof(HSSFWorkbook):typeof(XSSFWorkbook);
try {
wb = Activator.CreateInstance(t, new object[] {fs});
} catch (Exception ex) { MessageBox.Show("Problemi nella lettura del foglio\n" + ex.Message);}
if (wb != null) {
_SheetCount = ((IWorkbook)wb).Count;
sh = new ISheet[_SheetCount];
for (int i = 0; i < _SheetCount; i++) {
sh[i] = ((IWorkbook)wb).GetSheetAt(i);
Sheets.Append("\n" + sh[i].SheetName);
}
this._Sheet = Sheets.ToString().Substring(1).Split('\n');
}
fs.Close();
}
}
return Ret;
}
public ICell GetCell(int Row, int Col) {
ICell Ret;
Ret = GetCell(Row, Col, this.CurrentSheetIndex);
return Ret;
}
public ICell GetCell(int Row, int Col, int Sheet) {
ICell Ret;
Ret = sh[Sheet].GetRow(Row).GetCell(Col);
return Ret;
}
public String GetCellValue(int Row, int Col) {
String Ret;
Ret = GetCellValue(Row, Col, this.CurrentSheetIndex);
return Ret;
}
public string GetCellValue(int Row, int Col, int Sheet) {
string Ret;
Ret = sh[Sheet].GetRow(Row).GetCell(Col).ToString();
return Ret;
}
public IRow GetRow(int Row, int Sheet) {
IRow Ret;
Ret = sh[Sheet].GetRow(Row);
return Ret;
}
public IRow GetRow(int Row) {
IRow Ret;
Ret = GetRow(Row, CurrentSheetIndex);
return Ret;
}
}
|
|
|
|
|
Yes; interfaces were an option; wasn't sure if your design allowed for them. You can make things a bit more succinct:
IWorkbook wb;
...
wb = Activator.CreateInstance( t, new object[] { fs } ) as IWorkbook;
...
_SheetCount = wb.Count;
...
|
|
|
|
|
Thank you very much fof your suggestion
|
|
|
|
|
|
Hello,
my problem is to fill a DataGrid in WPF C# Framework 4.0.
Code DataGrid.xaml:
<Grid>
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True" HorizontalAlignment="Stretch" Margin="0,0,2,2" Name="dataGrid1" VerticalAlignment="Stretch" />
</Grid>
Code Page.cs :
public Page2()
{
InitializeComponent();
FillTable();
}
public class Lagerteile
{
public int ID { get; set; }
public DateTime Liefertermin { get; set; }
public string WANr { get; set; }
public string Name { get; set; }
public int Stk { get; set; }
public string Kunde { get; set; }
public string Pos { get; set; }
}
public void FillTable() {
string cs = "Driver={PostgreSQL Unicode};" +
"Server=localhost;" +
"Database=***;" +
"Uid=***;" +
"Pwd=***;";
string query = "SELECT *FROM xxxx";
OdbcConnection cn = null;
try
{
cn = new OdbcConnection(cs);
DataContext context = new DataContext(cn);
this.dataGrid1.DataContext = new ObservableCollection<Lagerteile>(
context.ExecuteQuery<Lagerteile>(query));
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
if (cn.State == ConnectionState.Open)
{
cn.Close();
cn.Dispose();
}
}
The Database is PostgreSQL 9.4. General it works but it took about 4 seconds to load 100 rows and only 1 second more for 15.000 rows. Is there any possibilty to speed this up ?
|
|
|
|
|
In WinForms (which this clearly isn't) you'd go for a virtual datagridview if the binding is too slow.
WPF might have something similar
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Is it the query or the binding to the ui that is causing the delay. Sounds like it is establishing the ODBC connection taking the bulk of the time!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thank you for your answers. I think so too. I tried a bit and found a solution which needs 1.5 seconds.
DataSet ds = new DataSet();
DataTable dt = new DataTable();
NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;User Id=xxx;" +
"Password=xxx;Database=xxx;");
conn.Open();
string query = "SELECT xxxx";
NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
NpgsqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
data.Add(new Lagerteile() { ID = (int)dr["ID"], Kunde = dr["Name"].ToString(), Liefertermin = (DateTime)dr["Liefertermin"], WANr = dr["WaNr"].ToString(), Name = dr["Bezeichnung"].ToString(), Pos = dr["Pos"].ToString(), Stk = (int)dr["Stk"] });
conn.Close();
dg123.DataContext = data;
It seems to be very long because you could not work in that time. Is there a possibility to fill it in the background that you get a result directly ?
How can I refresh the DataGrid ? The problem is that for example an other client changed the database.
modified 26-Dec-15 10:43am.
|
|
|
|
|
I am working on c# desktop application in visual studio 2013.
When i try to change startup form from program.cs file.It shows an exception.
Please help me to solve this issue.
|
|
|
|
|
What exception are you getting?
This space for rent
|
|
|
|
|
What is the exception?
Any message? Any inner exception?
Any code to give us a clue what you are doing?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
1. What are you trying to do?
2. Why would you want to change that (startup object)?
3. Not entirely sure, but it's probably because it would need a static void Main()
Best,
John
-- LogWizard - a Log Viewer that is easy and fun to use!
|
|
|
|