I have a project that it should print check bill in both excel and pdf format.
Originally I use NPOI to print excel and Spire to print pdf using the excel file:
public HSSFWorkbook Workbook;
public void SaveExcelFile(string FilePath)
{
this.AutoSizeColumn();
FilePath = FilePath == "" ? AppDomain.CurrentDomain.BaseDirectory + "sample.xls" : FilePath;
var File = new FileStream(FilePath, FileMode.Create);
Workbook.Write(File);
File.Close();
}
public void SavePdfFileFromExcel(string ExcelFilePath, string PdfFilePath)
{
Workbook pdfWorkbook = new Workbook();
pdfWorkbook.LoadFromFile(ExcelFilePath);
pdfWorkbook.SaveToFile(PdfFilePath, Spire.Xls.FileFormat.PDF);
}
But Spire will just print the pdf in portrait, and the bill will look small and ugly. I want to know if there's a way I can print the pdf in landscape.
Or I should use iText to create a pdf format by self?
Try like this:
public void ExcelLandscapeFormat()
{
try
{
((Microsoft.Office.Interop.Excel._Worksheet)
excelWbook.ActiveSheet).PageSetup.Orientation =
Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
excelWbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + output);
}
catch
{ }
}
Related
I am converting excel file .xlsx with phonetic text (Japanese text) but the output file gives out warning that there are unreadable contents, and the data are missing from the file.
It seems that the issues were because NPOI can't process the phonetic guide from Microsoft Excel in Japanese language.
This only applies on .xlsx files and not .xls files.
I'm not sure how to attach the sample excel file but the image above shows what it looks like. Basically, if you use Microsoft Excel in Japanese version, there will be automatic phonetic guide for every kanji (Chinese character).
public void writeoutput(string val)
{
try
{
IWorkbook book;
string outPath = "<path for input file>";
using (FileStream file = new FileStream(outPath, FileMode.Open, FileAccess.Read))
{
try
{
book = new XSSFWorkbook(file);
}
catch (Exception e)
{
book = null;
}
if (book == null)
{
book = new HSSFWorkbook(file);
}
file.Close();
}
//ISheet sheet = outputSheet.GetSheet("Sheet1");
ISheet sheet = book.GetSheetAt(0);
sheet.CopySheet("NewSheet", false);
IRow dataRow = sheet.GetRow(35) ?? sheet.CreateRow(35);
ICell cell = dataRow.GetCell(8) ?? dataRow.CreateCell(8);
cell.SetCellValue(val);
//File.Delete(outPath);
outPath = "<path for output file>";
using (FileStream file = new FileStream(outPath, FileMode.OpenOrCreate, FileAccess.Write))
{
book.Write(file);
file.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
textBox2.Text = "done";
}
}
I tried disable the phonetic guide in Microsoft Excel and the processing of the .xlsx file works with no issues but I can only disable the phonetic guide on the text one by one and it is very time consuming.
So this code down here works fine, it does the job, word -> PDF.
Only got a problem, when the word doc has tables with united cells, when converting the word doc to pdf the pdf show the united cells combine but with no value inside.
Someone knows why? Is the any solution for this?
NOTE: This is converting on server side.
if (nomeDocTemplate == attachment)
{
web.AllowUnsafeUpdates = true;
string wordEdit =item2.Attachments.UrlPrefix + "Template" + item.ID + ".docx";
string wordAutomationServiceName = "Word Automation Services";
string filenamedest = web.Url + item.File.ServerRelativeUrl;
SPFile attachmentFile = item2.Web.GetFile(wordEdit);
using (MemoryStream destinationStream = new MemoryStream())
{
//Call the syncConverter class, passing in the name of the Word Automation Service for your Farm.
SyncConverter sc = new SyncConverter(wordAutomationServiceName);
////Pass in your User Token or credentials under which this conversion job is executed.
sc.UserToken = SPContext.Current.Site.UserToken;
sc.Settings.UpdateFields = true;
//Save format
sc.Settings.OutputFormat = SaveFormat.PDF;
////Convert to PDF by opening the file stream, and then converting to the destination memory stream.
ConversionItemInfo info = sc.Convert(attachmentFile.OpenBinaryStream(), destinationStream);
var filename = Path.GetFileNameWithoutExtension(item.File.Name) + ".pdf";
if (info.Succeeded)
{
//File conversion successful, then add the memory stream to the SharePoint list.
SPFile newfile = web.Lists["Tramitar"].RootFolder.Files.Add(filename, destinationStream, true);
}
else if (info.Failed)
{
throw new Exception(info.ErrorMessage);
}
}
//ConcatAndAddContent(anexosProcesso);
return;
}
Here is the doc and the pdf, to understand better the problem I'm having:
WORD:
PDF:
I have Excel files in one folder in xlxs format.
The code should check each file for sheets starting with name 'help' and convert these sheets to a csv format file.
I am not able to get the format in the source work sheet to the csv converted file of that worksheet.
Please include the code line in the below code posted
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace Excel_CSV_Converter
{
class Program
{
[STAThread]
static void Main(string[] args)
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType == null)
{
Console.WriteLine("Excel is not installed");
}
else
{
// Let us continue our work on Excel file conversion.
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
// While saving, it asks for the user confirmation, whether we want to save or not.
// By setting DisplayAlerts to false, we just skip this alert.
app.DisplayAlerts = false;
FolderBrowserDialog fbd = new FolderBrowserDialog();
Console.WriteLine(#"Select the Source Directory from Dialog box where excel files present "); // Prompt
string SourceDir = string.Empty;
if (fbd.ShowDialog() == DialogResult.OK)
{
SourceDir = fbd.SelectedPath;
}
Console.WriteLine("Selected source directory is " + SourceDir + "\n");
Console.WriteLine(#"Select the Destination Directory where .csv files need to be saved"); // Prompt
string DestinationDir = string.Empty;
if (fbd.ShowDialog() == DialogResult.OK)
{
DestinationDir = fbd.SelectedPath;
}
Console.WriteLine("Selected Destination directory is " + SourceDir + "\n");
string[] files = System.IO.Directory.GetFiles(SourceDir + #"\", "*.XLSX");
// Now we open the upload file in Excel Workbook.
foreach (string file in files)
{
// This is to discard temporary excel files created in background.
if (file.Contains('$'))
{
continue;
}
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = app.Workbooks.Open(file);
string WorkbookName = excelWorkbook.Name;
foreach (Excel._Worksheet xlWorksheet1 in excelWorkbook.Sheets)
{
if (xlWorksheet1.Name.StartsWith("help"))
{
string newFileName = DestinationDir + #"\" + WorkbookName.Split('.')[0] + " - " + xlWorksheet1.Name.Split('-')[0] + ".csv";
Console.WriteLine("Created " + newFileName + "\n");
// Now save this file as CSV file.
xlWorksheet1.SaveAs(newFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV);
}
}
// Close the Workbook and Quit the Excel Application at the end.
excelWorkbook.Close();
}
app.Quit();
}
}
}
}
I am creating an excel file on the fly for sending it an an attachment in an email. The relevant code snippet is provided below (It's a console app)
public static void SendEmailWithExcelAttachment(DataTable dt)
{
try
{
string smptHost = smptTuple.Item1;
MailMessage mailMsg = new MailMessage();
.............................................
.............................................
byte[] data = GetData(dt);
//save the data to a memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
mailMsg.Attachments.Add(new System.Net.Mail.Attachment(ms, attachmentName, "application/vnd.ms-excel"));
....................................
....................................
//send email
smtpClient.Send(mailMsg); }
catch (Exception ex)
{
throw ex;
}
}
private static byte[] GetData(DataTable dt)
{
string strBody = DataTable2ExcelString(dt);
byte[] data = Encoding.ASCII.GetBytes(strBody);
return data;
}
private static string DataTable2ExcelString(System.Data.DataTable dt)
{
string excelSheetName = "Sheet1";
StringBuilder sbTop = new StringBuilder();
sbTop.Append("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ");
sbTop.Append("xmlns=\" http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=windows-1252\">");
sbTop.Append("<meta name=ProgId content=Excel.Sheet ><meta name=Generator content=\"Microsoft Excel 9\"><!--[if gte mso 9]>");
sbTop.Append("<xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + excelSheetName + "</x:Name><x:WorksheetOptions>");
sbTop.Append("<x:Selected/><x:ProtectContents>False</x:ProtectContents><x:ProtectObjects>False</x:ProtectObjects>");
sbTop.Append("<x:ProtectScenarios>False</x:ProtectScenarios></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>");
sbTop.Append("<x:ProtectStructure>False</x:ProtectStructure><x:ProtectWindows>False</x:ProtectWindows></x:ExcelWorkbook></xml>");
sbTop.Append("<![endif]-->");
sbTop.Append("</head><body><table>");
string bottom = "</table></body></html>";
StringBuilder sbHeader = new StringBuilder();
//Header
sbHeader.Append("<tr>");
for (int i = 0; i < dt.Columns.Count; i++)
{
sbHeader.Append("<td>" + dt.Columns[i].ColumnName + "</td>");
}
sbHeader.Append("</tr>");
//Items
for (int x = 0; x < dt.Rows.Count; x++)
{
sbHeader.Append("<tr>");
for (int i = 0; i < dt.Columns.Count; i++)
{
sbHeader.Append("<td>" + dt.Rows[x][i] + "</td>");
}
sbHeader.Append("</tr>");
}
string data = sbTop.ToString() + sbHeader.ToString() + bottom;
return data;
}
This works but when I tried to open the excel file from the attachement, I receive:
I checked an found some solution in SO Post but could not make it to work. I tried like <x:DisplayAlerts>False</x:DisplayAlerts> but didn't work.
If the file you want to create and send doesn't have to be in exactly ".xls" format.. and if you are comfortable with ".xlsx" format... I think you might wanna try with EPPlus library, as it was mentioned Here. As I said you have to work with ".xlsx" (you can work with other excel formats but you'll get the same message about the file format when you open the file). So you can create the Excel file in temp folder with EPPlus using the DataTable as sorce and send the temp file by email... something like this for example:
public static void SendEmailWithExcelAttachment(DataTable dt)
{
try
{
string smptHost = smptTuple.Item1;
MailMessage mailMsg = new MailMessage();
string temp = Path.GetTempPath(); // Get %TEMP% path
string file = "fileNameHere.xlsx";
string path = Path.Combine(temp, file); // Get the whole path to the file
FileInfo fi = new FileInfo(path);
using (ExcelPackage pck = new ExcelPackage(fi))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Table");
ws.Cells["A1"].LoadFromDataTable(dt, true);
pck.Save();
}
mailMsg.Attachments.Add(new System.Net.Mail.Attachment(path, "application/vnd.ms-excel"));
try
{
//send email
smtp.Send(mailMsg);
}
catch (Exception)
{
//do smth..
}
finally
{
File.Delete(path);
}
}
catch (Exception ex)
{
throw ex;
}
}
I created a simple DataTable, sent it to myself in ".xlsx" format and was able to open it without any "Format warnings". I hope that helps.
The warning is display by MS Excel application because your file is not a real Excel file. It is an HTML with XLS extension. An XLS file is a binary file. MS Excel recognizes the HTML file and it display the file in its spreadsheet grid.
MS Excel displays security warnings for files that comes from external sources like email or internet.
The best solution is to use an Excel library that saves real Excel files in xls (old Excel file format) or xlsx (new Excel file format).
You can choose between free libraries like NPOI, EPPlus or commercial libraries like EasyXLS. Some of them saves only xls files, other only xlsx files and a few of them supports both file formats.
Solution to the warning / error “The file you are trying to open is in a different format than specified by the file extension”
Cause :
This happens because in the traditional Export to Excel method, the GridView is first converted to an HTML string and then that HTML string is exported to Excel. Thus originally it is not an Excel file hence the Excel Application throws the warning / error “The file you are trying to open is in a different format than specified by the file extension”.
Solution :
The solution to this problem is using ClosedXML library which is wrapper over the DocumentFormat.OpenXml library.
Dependency : OpenXml SDK 2.0 must be installed in system
You can get more help here :)
I am using VS2005 C# and im trying to convert a pipe delimited text file to excel workbook format. Below is my code:
public partial class TextToExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SaveAsExcelBtn_Click(object sender, EventArgs e)
{
string xlExtension = ".csv";
string strExcelOutputFilename = "C:/Documents and Settings/rhlim/My Documents/" + DateTime.Now.ToString("yyyyMMddHHmmss") + xlExtension;
// Before attempting to import the file, verify
// that the FileUpload control contains a file.
if (TextFile.HasFile)
{
// Get the name of the Excel spreadsheet.
string strFileName = Server.HtmlEncode(TextFile.FileName);
// Get the extension of the text.
string strExtension = Path.GetExtension(strFileName);
// Validate the file extension.
if (strExtension != ".TXT" && strExtension!=".txt")
{
Response.Write("<script>alert('Failed to import. Cause: Invalid text file.');</script>");
return;
}
// Generate the file name to save the text file.
//string strUploadFileName = "C:/Documents and Settings/rhlim/My Documents/Visual Studio 2005/WebSites/SoD/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename)))
{
StreamReader inputReader = new StreamReader(TextFile.FileContent);
string fileContent = inputReader.ReadToEnd();
fileContent = fileContent.Replace('|', ';');
outputWriter.Write(fileContent);
TextFile.SaveAs(strExcelOutputFilename);
inputReader.Close();
}
//string strExcelOutputFilename = "C:/Documents and Settings/rhlim/My Documents/" + DateTime.Now.ToString("yyyyMMddHHmmss")+xlExtension;
// Save the Excel spreadsheet on server.
//TextFile.SaveAs (strExcelOutputFilename);
}
else Response.Write("<script>alert('Failed to import. Cause: No file found');</script>");
}
}
Currently I am having some file saving errors
Any suggestions? Thanks a lot!
That's because Excel doesnt support pipelines you have to convert it so comma's or semicolumns like:
using (StreamWriter outputWriter = new StreamWriter(File.Create(strExcelOutputFilename)))
{
StreamReader inputReader = new StreamReader(TextFile.FileContent);
string fileContent = inputReader.ReadToEnd();
fileContent = fileContent.Replace('|', ',');
outputWriter.Write(fileContent);
}
I googled and hope it will help you: http://csharp.net-informations.com/excel/csharp-create-excel.htm
Or, already answered: Create Excel (.XLS and .XLSX) file from C#
At the first link, the line xlWorkSheet.Cell[x,y] to put element in the dedicated cell.
FYI, xlsx format(new from Office 2007) will give you a great manipulation capability with code.
For generating and manipulating excel files, I personally prefer the NPOI library. Download it from Codeplex, add reference to the NPOI dlls to your project. Store a “template” excel file you would like in a known location, with the any column headers/formatting that you need. Then you just use npoi to make a copy of the template file and manipulate it at a sheet/row/column level and put whatever data you want.
The sample code snippet looks something like this. Assuming you have split your input into a List of strings
const string ExcelTemplateFile = "~/Resources/ExcelInputTemplate.xls";
const string ExcelWorksheetName = "Output Worksheet";
const int RequiredColumn = 1;
private HSSFWorkbook CreateExcelWorkbook(IEnumerable<String> inputData)
{
FileStream fs = new FileStream(Server.MapPath(ExcelTemplateFile), FileMode.Open, FileAccess.Read);
// Getting the complete workbook...
HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
// Getting the worksheet by its name...
HSSFSheet sheet = templateWorkbook.GetSheet(ExcelWorksheetName);
int startRowIterator = 1;
foreach (string currentData in inputData)
{
sheet.CreateRow(startRowIterator).CreateCell(RequiredColumn).SetCellValue(currentData);
}
}