|
<pre>
using Microsoft.Office.Interop.Excel;
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelTestConsoleApp1
{
class Program
{
public static string filePath = @"f:\ExcelTest\Tst_Excel.xlsx";
static void Main(string[] args)
{
CreateExcelFile();
ReadExcelFile();
Console.ReadLine();
}
private static void CreateExcelFile()
{
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
Console.WriteLine("Excel is not installed on this system... ");
return;
}
object misValue = System.Reflection.Missing.Value;
Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "FirstName";
xlWorkSheet.Cells[1, 2] = "LastName";
xlWorkSheet.Cells[2, 1] = "Greg";
xlWorkSheet.Cells[2, 2] = "Gonzales";
xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Excel file created successfully... ");
Console.BackgroundColor = ConsoleColor.Green;
}
private static void ReadExcelFile()
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("\nReading the Excel File... ");
Console.BackgroundColor = ConsoleColor.Green;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int totalRows = xlRange.Rows.Count;
int totalColumns = xlRange.Columns.Count;
string firstValue, secondValue;
for (int rowCount = 1; rowCount <= totalRows; rowCount++)
{
firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text);
secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text);
Console.WriteLine(firstValue + "\t" + secondValue);
}
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Console.WriteLine("End of File... ");
}
private static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
Console.WriteLine("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);
Every time I try to run this code I get this error…
Exception Unhandled
System.Runtime.InteropServices.COMExceptions:'This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type.'
<pre>
|
|
|
|
|
At a guess? You have an old version of Excel installed.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Quote: xlExcel12 50 Excel Binary Workbook *.xlsb
public static string filePath = @"f:\ExcelTest\Tst_Excel.xlsx";
XlFileFormat.xlExcel12,
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
|
hey all, beginner coder here! I have a project that I am going to attempt - I need to schedule an automated copy of files from several (10) client pcs to 1 main server. Any inputs on if I should use C# to build an executable that I could schedule on task scheduler to complete this? Or what would be the best to use? Thanks.
|
|
|
|
|
Yes: [^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
hi, i have some problems to use httpexception but, in my visual studio httpexception not found, can anyone help my problem to handle httpexception please?
|
|
|
|
|
|
i have a question, does HttpTransportBindingElement have an exception? if there is a way to use the exception like what?
|
|
|
|
|
It's a class. Classes don't throw exceptions - properties and methods *IN* that class do. If you want to find out what execptions are thrown by the methods and properties in that class, intellisense will tell you, and so will google.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I am trying to converting the HTML string to PDF file using IText, It through the following exception
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
string htmltext = "my html string";
byte[] pdf = null;
var workStream = new MemoryStream();
using (var pdfWriter = new PdfWriter(workStream))
{
pdfWriter.SetCloseStream(false);
using (var document = HtmlConverter.ConvertToDocument(htmltext, pdfWriter))
{
pdf = workStream.GetBuffer();
}
}
workStream.Position = 0;
return pdf;
|
|
|
|
|
Assuming this is iTestSharp you are using, not IText:
string htmlText = File.ReadAllText(@"D:\Test Data\APage.html");
using (MemoryStream stream = new System.IO.MemoryStream())
{
using (StringReader sr = new StringReader(htmlText))
{
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f))
{
using (PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream))
{
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
File.WriteAllBytes(@"D:\Test Data\APage.pdf", stream.ToArray());
}
}
}
}
Provided your HTML is well formed, that should work fine.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Call it several times, remembering where you found it last...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
1. you could post your question on the CP article you cite. while it appears the author hasn't participated in several years: it's worth trying.
2. this post on the article's message thread [^] should give you some useful ideas.
3. given the possibility of recursion (nested images of depth > 1): you need to plan for that case.
Make some effort on your own, and revise/edit your post here, adding code, and specific questions.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
|
Voted as abuse, since the display of stupidity is and might infect others.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Yes ! Please respond with complete credit card details, including PIN, or, if you use PayPal your UserName and Password.
Your software can be ready in a matter of weeks, and will run on Win, Mac, Android, Linux, and any IOT device; it will be able to handle any language now in use in the world. Of course, we will provide you lifetime priority support at no extra charge.
Don't worry about more than one of us accessing your funds: we have an honor system here where the first transponder handles all that.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
modified 30-Jun-19 1:06am.
|
|
|
|
|
No.
The only code you're ever going to get for this is the code YOU WRITE.
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Just saying "give me the code I need to hand is as my own work" is both unhelpful and stupid: you do realise that your tutor almost certainly knows about this site and will spot your plagiarism?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
l am stuck on the issue on how to enter data into different databases ..as in different databases for different restaurants
|
|
|
|
|
So how far have you got?
And why do you think different restaurants need different database? Possibly different tables, but most likely they would share a table in a single DB, with each row including a "RestaurantID" column which cross references (this is called a "Foreign Key") to a Restaurants table which holds the specific restaurant details such as ID, Name, Address, Head Chef, ...
You would "put them together" when you needed to using a JOIN.
So what design do you have, and why?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
SQL92.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
You wanna order from multiple restaurants?
How about duck feet, cheese burger and a side order of poutine?
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|