I've been trying to export the content data of my ListView control into an Excel application using C#. I've already done this before using VB.NET and I tried to convert my code to C# but it failed but I made a modification on it and seems everything is fine on code below except for the last part which to save the data as an excel file. I need an assistance to modify this code correctly. I would greatly appreciate your helpful response.
The code below that got an error is:
oBook.SaveAs(SaveFileDialog1.InitialDirectory.ToString() + SaveFileDialog1.FileName);
Error 5 No overload for method 'SaveAs' takes '1' arguments
private void Button4_Click(object sender, System.EventArgs e)
{
int row = 0;
int col = 0;
int row2 = 0;
int col2 = 0;
int ch = 0;
int ctr = 0;
ctr = 0;
row2 = 1;
col2 = 3;
row = 3;
col = 3;
Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
oExcel.Visible = false;
Microsoft.Office.Interop.Excel.Workbook oBook = oExcel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
Microsoft.Office.Interop.Excel.Worksheet oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oExcel.ActiveSheet;
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
SaveFileDialog1.Filter = "Excel File|*.xlsx|Word File|*.doc|Text File|*.txt";
SaveFileDialog1.Title = "Save As";
if (SaveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
switch (SaveFileDialog1.FilterIndex)
{
case 1:
{
for (ch = 1; ch <= ListView1.Columns.Count; ch++)
{
oSheet.Cells[row2, col2] = ListView1.Columns[ctr].Text;
col2 = col2 + 1;
ctr = ctr + 1;
}
foreach (ListViewItem lview in ListView1.Items)
{
foreach (ListViewItem.ListViewSubItem lview2 in lview.SubItems)
{
oSheet.Cells[row, col] = lview2.Text;
col = col + 1;
}
col = 3;
row = row + 1;
}
oBook.SaveAs(SaveFileDialog1.InitialDirectory.ToString() + SaveFileDialog1.FileName);
oExcel.Quit();
SaveFileDialog1.Dispose();
MessageBox.Show("Data has been successfully saved", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
The workbook SaveAs method takes 11 parameters. In C# you have to pass something for these optional parameters even if you do not care about them. The easiest thing to do here is pass System.Reflection.Missing.Value for those you do not care about. It is a pain though.
Related
I'm trying to read my excel files saved in my azure storage container like this
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("concursos");
foreach (BlobItem blobItem in containerClient.GetBlobs())
{
BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var stream=blobClient.OpenRead(new BlobOpenReadOptions(true)))
using (ExcelPackage package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
}
}
But the sentence
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
throws me an error:System.NullReferenceException: 'Object reference not set to an instance of an object.' worksheet was null
I debug an see fine my stream an my package
The excels in blobs are like this one .xls
Any idea, please?
Thanks
Please check if worksheet is empty .This error occurs if there is empty sheet with empty coumns and rows.
I tried to reproduce the same
Initially I tried to read a excel sheet with EPplus , where starting column and rows are filled and not empty and could execute and read successfully using the same code as yours.
Then I removed column1 to be empty and stored in blob and tried to read it and got null reference exception.
The Dimension object of the ExcelWorksheet will be null if the worksheet was just initialized and is empty .
And so throws null reference exception, AFAIK , the only way is to check if files are empty or to add content to it before accessing them so that if columns are empty , it would not throw exception.
worksheet.Cells[1, 1].Value = "Some text value";
Same way try to add worksheet, to avoid exception if in case there are no sheets in container blob.
ExcelWorksheet worksheet = new ExcelPackage().Workbook.Worksheets.Add("Sheet1");
This code will not throw an exception since the Dimension object was initialized by adding content to the worksheet.If the loaded
ExcelWorksheet already contains data, you will not face this issue.
ExcelWorksheet worksheet = package.Workbook.Worksheets.First();
//or ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
// Add below line to add new sheet , if no sheets are present and returning null exception
//ExcelWorksheet worksheet = new ExcelPackage().Workbook.Worksheets.Add("Sheet1");
//Add below line to add column and row , if sheet is empty and returning null exception
worksheet.Cells[1, 1].Value = " This is the end of worksheet";
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
}
}
You can alternatively check if the value is null.
if(worksheet.cells[row,column].value != null)
{
//proceed with code
}
The problem was the file extension of the excel files in blobs
Only works fone with .xlsx not with .xls
Thanks
Inside a for loop I have my 'Page companies' object which stores the result of method called 'findByNameOrGroupIdOrTerritoryIdAndCompanyIdsInobject' 'n' number of times. If it was a List we could go with companies.addAll() method so that it would append to the same object.
Is there any way to append to companies object instead of assigning every time which only stores last iteration result?
Here is the code snippet:
Page<Company> companies = null;
int parametersLimit = 500;
int companyBatches = companyIds.size() / parametersLimit;
for (int companyBatchIndex = 0; companyBatchIndex <= companyBatches; companyBatchIndex++) {
int lowerIndex = companyBatchIndex * parametersLimit;
int upperIndex = Math.min((companyBatchIndex + 1) * parametersLimit, companyIds.size());
companies = companyRepository.findByNameOrGroupIdOrTerritoryIdAndCompanyIdsIn(nameOrGroupId, territoryId, companyIds.subList(lowerIndex, upperIndex), pageable);
}
Thank you!
Simply use a list you should not use page like this
Simply put, there is no such way to append to Page contents. So I modified the logic to return List of Companies and then using PageImpl I converted the List to Page.
Page<Company> companies = null;
List<Company> companiesList = new ArrayList<Company>();;
int parametersLimit = 500;
int companyBatches = companyIds.size() / parametersLimit;
for (int companyBatchIndex = 0; companyBatchIndex <= companyBatches; companyBatchIndex++) {
int lowerIndex = companyBatchIndex * parametersLimit;
int upperIndex = Math.min((companyBatchIndex + 1) * parametersLimit, companyIds.size());
companiesList.addAll(companyRepository.findByNameOrGroupIdOrTerritoryIdAndCompanyIdsIn(nameOrGroupId, territoryId, companyIds.subList(lowerIndex, upperIndex), pageable));
}
int fromIndex = pageable.getPageNumber() * pageable.getPageSize();
int toIndex = Math.min( (int) (pageable.getOffset() + pageable.getPageSize()), companiesList.size());
companies = new PageImpl<>(companiesList.subList(fromIndex, toIndex), pageable, companiesList.size());
This InDesign Javascript iterates over textStyleRanges and converts text with a few specific appliedFont's and later assigns a new appliedFont:-
var textStyleRanges = [];
for (var j = app.activeDocument.stories.length-1; j >= 0 ; j--)
for (var k = app.activeDocument.stories.item(j).textStyleRanges.length-1; k >= 0; k--)
textStyleRanges.push(app.activeDocument.stories.item(j).textStyleRanges.item(k));
for (var i = textStyleRanges.length-1; i >= 0; i--) {
var myText = textStyleRanges[i];
var converted = C2Unic(myText.contents, myText.appliedFont.fontFamily);
if (myText.contents != converted)
myText.contents = converted;
if (myText.appliedFont.fontFamily == 'Chanakya'
|| myText.appliedFont.fontFamily == 'DevLys 010'
|| myText.appliedFont.fontFamily == 'Walkman-Chanakya-905') {
myText.appliedFont = app.fonts.item("Utsaah");
myText.composer="Adobe World-Ready Paragraph Composer";
}
}
But there are always some ranges where this doesn't happen. I tried iterating in the forward direction OR in the backward direction OR putting the elements in an array before conversion OR updating the appliedFont in the same iteration OR updating it a different one. Some ranges are still not converted completely.
I am doing this to convert the Devanagari text encoded in glyph based non-Unicode encoding to Unicode. Some of this involves repositioning vowel signs etc and changing the code to work with find/replace mechanism may be possible but is a lot of rework.
What is happening?
See also: http://cssdk.s3-website-us-east-1.amazonaws.com/sdk/1.0/docs/WebHelp/app_notes/indesign_text_frames.htm#Finding_and_changing_text
Sample here: https://www.dropbox.com/sh/7y10i6cyx5m5k3c/AAB74PXtavO5_0dD4_6sNn8ka?dl=0
This is untested since I'm not able to test against your document, but try using getElements() like below:
var doc = app.activeDocument;
var stories = doc.stories;
var textStyleRanges = stories.everyItem().textStyleRanges.everyItem().getElements();
for (var i = textStyleRanges.length-1; i >= 0; i--) {
var myText = textStyleRanges[i];
var converted = C2Unic(myText.contents, myText.appliedFont.fontFamily);
if (myText.contents != converted)
myText.contents = converted;
if (myText.appliedFont.fontFamily == 'Chanakya'
|| myText.appliedFont.fontFamily == 'DevLys 010'
|| myText.appliedFont.fontFamily == 'Walkman-Chanakya-905') {
myText.appliedFont = app.fonts.item("Utsaah");
myText.composer="Adobe World-Ready Paragraph Composer";
}
}
A valid approach is to use hyperlink text sources as they stick to the genuine text object. Then you can edit those source texts even if they were actually moved elsewhere in the flow.
//Main routine
var main = function() {
//VARS
var doc = app.properties.activeDocument,
fgp = app.findGrepPreferences.properties,
cgp = app.changeGrepPreferences.properties,
fcgo = app.findChangeGrepOptions.properties,
text, str,
found = [], srcs = [], n = 0;
//Exit if no documents
if ( !doc ) return;
app.findChangeGrepOptions = app.findGrepPreferences = app.changeGrepPreferences = null;
//Settings props
app.findChangeGrepOptions.properties = {
includeHiddenLayers:true,
includeLockedLayersForFind:true,
includeLockedStoriesForFind:true,
includeMasterPages:true,
}
app.findGrepPreferences.properties = {
findWhat:"\\w",
}
//Finding text instances
found = doc.findGrep();
n = found.length;
//Looping through instances and adding hyperlink text sources
//That's all we do at this stage
while ( n-- ) {
srcs.push ( doc.hyperlinkTextSources.add(found[n] ) );
}
//Then we edit the stored hyperlinks text sources 's texts objects contents
n = srcs.length;
while ( n-- ) {
text = srcs[n].sourceText;
str = text.contents;
text.contents = str+str+str+str;
}
//Eventually we remove the added hyperlinks text sources
n = srcs.length;
while ( n-- ) srcs[n].remove();
//And reset initial properties
app.findGrepPreferences.properties = fgp;
app.changeGrepPreferences.properties = cgp;
app.findChangeGrepOptions.properties =fcgo;
}
//Running script in a easily cancelable mode
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Disclaimer: I am Newb. I understand scripting a little, but writing it is a pain for me, mostly with loops and arrays, hence the following.
I am attempting to pull all of the data from a specific column (in this case H [8]), check each cell's value in that column and if it is a y, change it to Yes; if it's n, change it to No; if it's empty, leave it alone and move onto the next cell.
Here's what I have so far. As usual, I believe I'm pretty close, but I can't set the value of the active cell and I can't see where I'm messing it up. At one point I actually changed ever value to Yes in the column (so thankful for undo in these cases).
Example of Sheet:
..... COL-H
r1... [service] <-- header
r2... y
r3... y
r4... n
r5... _ <-- empty
r6... y
Intent: Change all y's to Yes and all n's to No (skip blank cells).
What I've tried so far:
Function attempt 1
function Thing1() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("mySheet");
var lrow = ss.getLastRow();
var rng = ss.getRange(2, 8, lrow - 1, 1);
var data = rng.getValues();
for (var i=0; i < data.length; i++) {
if (data[i][0] == "y") {
data[i][0] == "Yes";
}
}
}
Function attempt 2
function Thing2() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("mySheet");
var lrow = ss.getLastRow();
var rng = ss.getRange(2, 8, lrow - 1, 1);
var data = rng.getValues();
for (var i=0; i < data.length; i++) {
if (data[i][0] == "n") {
data.setValue("No");
} else if (data[i][0] == "y") {
data.setValue("Yes");
}
}
}
Usage:
Once I'm done here, I want to modify the function so that I can target any column and change one value to another (I already have a method for that, but I need to be able to set the value). It would be like so: =replace(sheet, col, orig_value, new_value). I will post it as well below.
Thanks in advance for the help.
Completed Code for searching and replacing within a column
function replace(sheet, col, origV1, newV1, origV2, newV2) {
// What is the name of the sheet and numeric value of the column you want to search?
var sheet = Browser.inputBox('Enter the target sheet name:');
var col = Browser.inputBox('Enter the numeric value of the column you\'re searching thru');
// Add old and new targets to change (Instance 1):
var origV1 = Browser.inputBox('[Instance 1:] What old value do you want to replace?');
var newV1 = Browser.inputBox('[Instance 1:] What new value is replacing the old?');
// Optional - Add old and new targets to change (Instance 2):
var origV2 = Browser.inputBox('[Instance 2:] What old value do you want to replace?');
var newV2 = Browser.inputBox('[Instance 2:] What new value is replacing the old?');
// Code to search and replace data within the column
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet);
var lrow = ss.getLastRow();
var rng = ss.getRange(2, col, lrow - 1, 1);
var data = rng.getValues();
for (var i=0; i < data.length; i++) {
if (data[i][0] == origV1) {
data[i][0] = newV1;
} else if (data[i][0] == origV2) {
data[i][0] = newV2;
}
}
rng.setValues(data);
}
Hope this helps someone out there. Thanks Again #ScampMichael!
The array named data was created from the values in the range and is independent of the spreadsheet after it is created so changing an element in the array does not affect the spreadsheet. You must modify the array and then put the whole array back where it came from.
for (var i=0; i < data.length; i++) {
if (data[i][0] == "n") {
data[i][0] = "No";
} else if (data[i][0] == "y") {
data[i][0] = "Yes";
}
}
rng.setValues(data); // replace old data with new
}
I have created pdf from HTML page using Abc PDF now my problem is I want to print a table header on next page, but only if table data is display within a another page, if not display header on a different page, any one have idea about how we can do this using Abc pdf.
What you need to do is create the page with some space at the top, then once your document is built in abc PDF loop though the pages and add a header.
The code below is what I use to add a header, the header in this case has three bits a image at the top and two boxes with text in.
Remember the cord in abc pdf are from the bottom right not the top left.
private static Doc AddHeader(Doc theDoc, Core.Property propertyDetails)
{
int theCount = theDoc.PageCount;
int i = 0;
//Image header
for (i = 1; i <= theCount; i++)
{
theDoc.Rect.Width = 590;
theDoc.Rect.Height = 140;
theDoc.Rect.Position(0, 712);
theDoc.PageNumber = i;
//Check Which office to use.
string imagefilePath = HttpContext.Current.Server.MapPath("/images/pdf/pdf-header.png");
Bitmap myBmp = (Bitmap)Bitmap.FromFile(imagefilePath);
theDoc.AddImage(myBmp);
}
//page header boxes.
//Grey header box
theDoc.Rect.String = "20 15 590 50";
theDoc.Rect.Position(13, 672);
System.Drawing.Color colour = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
theDoc.Color.Color = colour;
theDoc.PageNumber = 1;
theDoc.FillRect();
theDoc.Rect.String = "20 15 586 50";
theDoc.Rect.Position(30, 660);
System.Drawing.Color pageoneText = System.Drawing.ColorTranslator.FromHtml("#50474A");
theDoc.Color.Color = pageoneText;
string thePageFont = "Century Gothic";
theDoc.Font = theDoc.AddFont(thePageFont);
theDoc.FontSize = 16;
theDoc.PageNumber = 1;
theDoc.AddText("My Text!!!!!");
theDoc.Rect.String = "20 15 590 50";
theDoc.Rect.Position(13, 630);
System.Drawing.Color greyBox = System.Drawing.ColorTranslator.FromHtml("#468DCB");
theDoc.Color.Color = greyBox;
theDoc.PageNumber = 1;
theDoc.FillRect();
theDoc.Rect.String = "20 15 586 50";
theDoc.Rect.Position(30, 620);
System.Drawing.Color greyText = System.Drawing.ColorTranslator.FromHtml("#ffffff");
theDoc.Color.Color = greyText;
string thePageFontTwo = "Century Gothic";
theDoc.Font = theDoc.AddFont(thePageFontTwo);
theDoc.FontSize = 14;
theDoc.PageNumber = 1;
theDoc.AddText("This is more text");
return theDoc;
}
Then once the pdf file is created just call
var theDoc = new Doc();
/// Your document creation stuff!!!
theDoc = AddHeader(theDoc, propertyDetails);