|
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!
|
|
|
|
|
using this lib System.IO.Compression i want to compress file in multivolumen mode, compress large file in several small files.
i want to use .net library and not external lib.
thanks.
|
|
|
|
|
That's nice. Good luck.
If it's not broken, fix it until it is
|
|
|
|
|
but do you know how to do that??
|
|
|
|
|
He probably does know how to do it, now that you mention it.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
What you're missing is that you haven't asked a question at all. You didn't say anything about what the problem your having, what you tried, what problems you ran into, ..., nothing at all.
|
|
|
|
|
ok, my problem is this, i can compress/uncompress file using the lib system.io.compression, but i dont know how to do this using into multivolumen file, i mean i want to compress a large file into several small file. how can i do this using .net library?
|
|
|
|
|
These smaller files, do they have to conform to a standard or can they just represent a large chunk of binary split into chunks? Also, what framework are you using (2,3,4 etc.) as they've added quit a bit to that namespace of late.
Regards,
Rob Philpott.
|
|
|
|
|
easy i want to do the split into volumen size that winrar do. for example i have a file that have 200mb and i want to compress it into two file of 100mb.
|
|
|
|
|
Hmm. OK, on the assumption that you are compressing one file, and that the separate chunk files are not expected to interop with things like WinZip or WinRar it would be fairly straight forward.
- Open the source file with a FileStream
- Chain a DeflateStream onto the filestream (just pass in constructor)
- Read from the deflate stream the number of bytes you want in your chunks (into byte array)
- Write entire byte array to individual file.
- Goto 3 (until end of filestream).
Going the other way is a bit more tricky, as you can't really create one stream from a set of files, but works in a similar fashion.
Regards,
Rob Philpott.
|
|
|
|
|
finally someone, thanks i'll try waht you tell me. i dont know what its DeflateStream but i'll make a search on the net for it. if you any example i'll thanksful too.
and yes am compressing only one file.
thanks
|
|
|
|
|
|
that class is what i want to use, but i don't know how to split a file in several file. someone can tell me how using zip class can split a single file into several file?
thanks
|
|
|
|
|
Follow the link I gave you and read the documentation.
|
|
|
|