- PUSHD <UNC PATH> will create a temporary virtual drive and get into it
- POPD will delete the temporary drive and get you back to the path you were when you entered pushd
“No problem can be solved from the same level of consciousness that created it.” Albert Einstein (1879-1955)
Wednesday, 29 November 2017
Browse UNC path using Windows CMD
Saturday, 25 November 2017
Sunday, 19 November 2017
Thursday, 16 November 2017
Convert HTML to Docx and Pdf using Microsoft.Office.Interop.Word
// use _Application and _Document instead of var to avoid ambiguities
_Application word = new Application();
_Document document = word.Documents.Open(FileName: @"C:\index.html", ConfirmConversions: false, ReadOnly: false);
word.Visible = false;
// impersonated user must have read-write access to the following locations
var pdfOutputFile = @"C:\Temp\document.pdf";
var docOutputFile = @"C:\Temp\document.docx";
byte[] Content = null;
.
.
.
case "pdf":
document.SaveAs2(FileName: pdfOutputFile, FileFormat: WdSaveFormat.wdFormatPDF);
document.Close();
word.Quit();
Content = File.ReadAllBytes(pdfOutputFile);
break;
case "docx":
foreach (InlineShape image in document.InlineShapes)
{
// make sure images are embeded
if (image.LinkFormat != null)
{
try
{
image.LinkFormat.SavePictureWithDocument = true;
image.LinkFormat.BreakLink();
}
catch { /* do nothing */ }
}
}
document.SaveAs2(FileName: docOutputFile, FileFormat: WdSaveFormat.wdFormatDocumentDefault);
document.Close();
word.Quit();
Content = File.ReadAllBytes(docOutputFile);
break;
.
.
.
// download file as byte[]
if (Content != null)
{
Response.AddHeader("content-disposition", "attachment; filename=" + (pdfOutputFile or docOutputFile));
Response.BufferOutput = true;
Response.OutputStream.Write(Content, 0, Content.Length);
Response.End();
}
Important notes:
_Application word = new Application();
_Document document = word.Documents.Open(FileName: @"C:\index.html", ConfirmConversions: false, ReadOnly: false);
word.Visible = false;
// impersonated user must have read-write access to the following locations
var pdfOutputFile = @"C:\Temp\document.pdf";
var docOutputFile = @"C:\Temp\document.docx";
byte[] Content = null;
.
.
.
case "pdf":
document.SaveAs2(FileName: pdfOutputFile, FileFormat: WdSaveFormat.wdFormatPDF);
document.Close();
word.Quit();
Content = File.ReadAllBytes(pdfOutputFile);
break;
case "docx":
foreach (InlineShape image in document.InlineShapes)
{
// make sure images are embeded
if (image.LinkFormat != null)
{
try
{
image.LinkFormat.SavePictureWithDocument = true;
image.LinkFormat.BreakLink();
}
catch { /* do nothing */ }
}
}
document.SaveAs2(FileName: docOutputFile, FileFormat: WdSaveFormat.wdFormatDocumentDefault);
document.Close();
word.Quit();
Content = File.ReadAllBytes(docOutputFile);
break;
.
.
.
// download file as byte[]
if (Content != null)
{
Response.AddHeader("content-disposition", "attachment; filename=" + (pdfOutputFile or docOutputFile));
Response.BufferOutput = true;
Response.OutputStream.Write(Content, 0, Content.Length);
Response.End();
}
Important notes:
- In order to be able to save document, create a new "Desktop" directory inside of "C:\Windows\SysWOW64\config\systemprofile\"
- The library will not work on Server 2016 with O365 or Office 2016. On server 2016, need to use Office 2013.
Tuesday, 31 October 2017
MS-SQL: Strip NON-Alphabetic Characters From String
Create Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
Declare @KeepValues as varchar(50)
Set @KeepValues = '%[^a-z]%'
While PatIndex(@KeepValues, @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')
Return @Temp
End
TEST:
Select dbo.RemoveNonAlphaCharacters('abc1234def5678ghi90jkl')
Friday, 22 September 2017
Create & Download PDF from Byte[] Array using jQuery AJAX and WebMethod
//convert BASE64 string to Byte{} array
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
//save Byte[] array and download
function saveByteArray(reportName, byte) {
var blob = new Blob([byte]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = reportName + ".pdf";
link.download = fileName;
link.click();
}
//in this case we are exporting a post based on id
function export(id) {
$.ajax({
type: "POST",
url: "Default.aspx/ConvertToPdfAndDownload",
data: '{postId: "' + id + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d == '')
alert('There is a problem exporting the file');
else {
var sampleArr = base64ToArrayBuffer(response.d);
saveByteArray("File-" + id, sampleArr);
}
},
failure: function (response) {
alert("Cannot export thefile: Error in calling Ajax");
}
});
}
// C# code behind
// using Pdf generator from https://www.nrecosite.com/pdf_generator_net.aspx
[WebMethod]
public static string ConvertToPdfAndDownload(string postId)
{
string result = "";
try
{
// convert HTML to PDF Byte[] array and return as Base64 string to AJAX
string post = "<h2>Here is a test</h2>";
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
result = Convert.ToBase64String(htmlToPdf.GeneratePdf(post));
}
catch (Exception e) { result = e.Message; }
return result;
}
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
//save Byte[] array and download
function saveByteArray(reportName, byte) {
var blob = new Blob([byte]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = reportName + ".pdf";
link.download = fileName;
link.click();
}
//in this case we are exporting a post based on id
function export(id) {
$.ajax({
type: "POST",
url: "Default.aspx/ConvertToPdfAndDownload",
data: '{postId: "' + id + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d == '')
alert('There is a problem exporting the file');
else {
var sampleArr = base64ToArrayBuffer(response.d);
saveByteArray("File-" + id, sampleArr);
}
},
failure: function (response) {
alert("Cannot export thefile: Error in calling Ajax");
}
});
}
// C# code behind
// using Pdf generator from https://www.nrecosite.com/pdf_generator_net.aspx
[WebMethod]
public static string ConvertToPdfAndDownload(string postId)
{
string result = "";
try
{
// convert HTML to PDF Byte[] array and return as Base64 string to AJAX
string post = "<h2>Here is a test</h2>";
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
result = Convert.ToBase64String(htmlToPdf.GeneratePdf(post));
}
catch (Exception e) { result = e.Message; }
return result;
}
Thursday, 14 September 2017
C# ASP.NET Convert DataTable to CSV and Download
StringBuilder sb = new StringBuilder();
DataTable dt = ds.Tables[0];
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row
.ItemArray
.Select(field => string
.Concat("\"", field
.ToString()
.Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "inline;filename=export.csv");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "text/csv";
Response.Write(sb);
Response.End();
DataTable dt = ds.Tables[0];
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row
.ItemArray
.Select(field => string
.Concat("\"", field
.ToString()
.Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "inline;filename=export.csv");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "text/csv";
Response.Write(sb);
Response.End();
Subscribe to:
Posts (Atom)
Analysis Service in Power BI Report Server
We couldn’t connect to the Analysis Services server. Make sure you’ve entered the connection string correctly... link
-
//convert BASE64 string to Byte{} array function base64ToArrayBuffer(base64) { var binaryString = window.atob(base64); var binar...
-
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter(); htmlToPdf.PageFooterHtml = @"<div style='text-align:right; font-s...
-
static void Main(string[] args) { // create a dummy list List<string> data = GetTheListOfData(); // split the lis...