If I run the following code with the second line commented out as shown, no exceptions are thrown. If I comment out the first line and uncomment the second line, ExcelPackage.GetAsByteArray() throws exception "Cannot access a closed Stream". What am I doing wrong?
public byte[] GetSpreadsheetByteArray()
{
using (ExcelPackage excelPackage = new ExcelPackage(new FileInfo(Server.MapPath(#"Config\StarterSpreadsheet.xlsm")),true))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("MySheet");
//ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Copy("Sheet1","MySheet");
return excelPackage.GetAsByteArray();
}
}
Related
I'm looking to serialise individual PowerPoint slides and persist them on disk. The goal is to access the slide data later on and add them to other presentations. For our add-in, the number of slides to be persisted could grow fast and we'd rather not store them in PowerPoint files for performance reasons.
I've tried to persist the data in the clipboard for office 2010-2016 by getting the data format "PowerPoint 14.0 Slides Package" from the clipboard after copying a slide. I then read the files, set them back to the clipboard and paste them in the Slides collection of the target presentation.
But I'm not able to make this work for Office 2007 - there's no "slides package" format in the memory. The only clipboard clip that looks like it could store the slide data is "Embedded Object" but that doesn't seem to work as I'm not able to paste the slide again.
Is there a way to solve this? And am I missing a more elegant solution to the problem?
Here is my current code in C#:
public static string PptSlideClipFormat = "PowerPoint 14.0 Slides Package";
public static void serializeSlide(PowerPoint.Slide slide, string filePath)
{
slide.Copy();
var dataObject = Clipboard.GetDataObject();
MemoryStream myStream = (MemoryStream)Clipboard.GetData(PptSlideClipFormat);
IsolatedStorageFileStream fs = new IsolatedStorageFileStream(filePath, FileMode.Create, Utils.Common.isoStore);
try
{
myStream.WriteTo(fs);
}
catch
{
throw new Exception("Failed to serialize slide clipboard data");
}
finally
{
fs.Close();
}
Utils.Common.clearClipboard();
}
[…]
public static void deserializeSlide(string filePath)
{
IsolatedStorageFileStream fs = new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate, Utils.Common.isoStore);
try
{
Clipboard.SetData(PptSlideClipFormat, fs);
}
catch
{
throw new Exception("Failed to serialize slide clipboard data");
}
finally
{
fs.Close();
}
}
I have a SSIS package to perform incremental processing of the cube. In the sequence container in this package, we have a script task to count the number of rows.
and the code in script task is as follows :
public void Main()
{
string connectionString = "Data Source=localhost;PROVIDER=MSOLAP;Impersonation Level=Impersonate;Catalog=EcovaPlatform";
connectionString = connectionString.Replace("localhost", Dts.Variables["User::CubeServer1"].Value.ToString()).Replace("EcovaPlatform", Dts.Variables["User::CubeName1"].Value.ToString());
string queryString = "evaluate row(\"count\", countrows(BillDetail))";
AdomdConnection connection = new AdomdConnection(connectionString);
connection.Open();
AdomdCommand cmd = new AdomdCommand(queryString);
cmd.Connection = connection;
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Dts.Variables["CubeBillDetailRowCount1"].Value = Convert.ToInt64(reader[0]);
}
}
Dts.TaskResult = (int)ScriptResults.Success;
But every once in a while, this script task(here I am assuming its the script task) fails. It throws out the following error:
Source Name: Cube Table RowCount
Error Code: 1
Error Description: Exception has been thrown by the target of an invocation.
Now I dont know how to fix this issue. So I am turning to you good folks, to guide me in the right direction.
If somebody wants to look at the XMLA script, I can post that as well.
Thanks.
i'm trying to get a PDF report using Jasper in my Java web application but i'm facing to an null pointer exception and i'm not able to find which is the error.
here below my code :
private void caricaReport() {
try{
InputStream is = getClass().getResourceAsStream("reports/miooperearte.jasper");
File OutDir = new File(outputDir);
File outDir = new File(outputDir);
outDir.mkdirs();
OutputStream os = new FileOutputStream(new File(outDir, "testReportNadia.pdf"));
HashMap parameterMap = new HashMap();
parameterMap.put("immagini_base_dir", "/Applications/MAMP/htdocs/Dboperearte/app/webroot/images/");
Collection data = leggiOpere();
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(data,false);
JasperRunManager.runReportToPdfStream(is, os, parameterMap, dataSource);
}
catch (Exception e) {
e.printStackTrace();
}
}
variables "is", "os", "parameterMap" and "dataSource" are all filled, exception doesn't show which is the null problem only say null pointer exception...
any idea which can help me to solve or find the problem ?
Thanks
I would guess that the parameterMap doesn't contain an entry for something that the JasperRunManager is expecting - make sure you aren't missing any values from there.
I'm facing org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save:an error occurs while saving the package : The part /docProps/app.xml fail to be saved in the stream with marshaller <br/> org.apache.poi.openxml4j.opc.internal.marshallers.DefaultMarshaller#7c81475b
Exception when try to write the each test scenario result(PASS or FAIL) into Excel sheet(.xlsx) after the each test scenario execution completion. I write the following two different modules for this purpose.
Please tell me where is the problem and how to resolve it..
//Method for writing results into Report
public void putResultstoReport(String values[])
{
int j=NoofTimesExecuted;
NoofTimesExecuted++;
XSSFRow row = sheet.createRow(j);
for(int i=0;i<values.length;i++)
{
XSSFCell cell = row.createCell(i);
cell.setCellValue(values[i]);
}
try {
System.out.println("Times:"+NoofTimesExecuted);
wb.write(fileOut);
}
//fileOut.flush();
//fileOut.close();
}
catch(Exception e) {
System.out.println("Exception at closing opened Report :"+e);
}
//Method for Creating the Excelt Report
public void createReport()
{
String FileLocation = getProperty("WorkSpace")+"//SCH_Registration//OutPut//TestResults.xlsx";
try {
fileOut = new FileOutputStream(FileLocation);
String sheetName = "TestResults"; //name of sheet
wb = new XSSFWorkbook();
sheet = wb.createSheet(sheetName);
fileOut.flush();
fileOut.close();
}
catch(Exception e)
{
System.out.println("Exception at Create Report file:"+e);
}
}
I had this problem today and fixed it already.
The problem is in putResultstoReport()
You can't wb.write(fileOut); in your cycle.
resolution:
first call putResultstoReport(); then wb.write(fileOut);
I also had this error.
I found my mistake was caused because I was opening the same file / workbook multiple times.
So I would recommend to make sure you are opening just once before attempting to close as well.
This can happen if a timeout occurs. I have code that works for a small dataset and throws this error with huge dataset.
I had the same issue.
When I shortened the output excel filename, it stopped.
I had similar Issue.
Finally I got the reason and that was version for the below jar file was getting overrided.
org.apache.xmlgraphics:batik-dom
Hence, I added below dependency and now it is working fine.
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-dom</artifactId>
<version>1.8</version>
</dependency>
This jar contains dependency for xalan.
To generate the report xalan is required.
I had the same problem user refreshing page and sending the request again before the previous request is completed.
when creating the name use millisecond in name to avoid name conflict with these updates in code of name creation resolved the above issue.
String sheetName="projectName"+System.currentTimeMillis() + ".xlsx"
FileOutputStream fileOut = new FileOutputStream(sheetName);
workbook.write(fileOut);
fileOut.close();
I have written an external application to drive autocad with a dll that was registered for COM. I have followed this codes to write my application however i have replaced the following code with AddNumbers() method:
public string OpenDWGFile(string MyDWGFilePath)
{
DocumentCollection dm = Application.DocumentManager;
Document doc = null;
if(File.Exists(MyDWGFilePath))
{
doc = dm.Open(MyDWGFilePath, false);
Application.DocumentManager.MdiActiveDocument = doc;
return "This file is exists";
}
else
return "This file is not exist";
}
but when i run my application the autocad software open and then close immediatly and this error message is shown : Exception has been thrown by the target of an invocation.
but if i comment the following lines of my code the application works without any errors:
doc = dm.Open(MyDWGFilePath, false);
Application.DocumentManager.MdiActiveDocument = doc;
You are creating a second instance of the DocumentManager and giving it a reference to an object retrieved from the first one. I think you want to use
dm.MdiActiveDocument = doc;