1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
package Tutorial02;
import System.Console;
import System.Data.*;
import EasyXLS.*;
import EasyXLS.Constants.*;
import System.Drawing.*;
public class Tutorial02
{
public Tutorial02()
{
}
public static void main(String[] args)
{
Console.WriteLine("Tutorial 02\n-----------\n");
ExcelDocument workbook = new ExcelDocument();
String sConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
System.Data.SqlClient.SqlConnection sqlConnection =
new System.Data.SqlClient.SqlConnection(sConnectionString);
sqlConnection.Open();
String sQueryString = "SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) + " +
"'/' + CAST(Day(ord.OrderDate) AS varchar) + " +
"'/' + CAST(year(ord.OrderDate) AS varchar) AS 'Order Date', " +
"P.ProductName AS 'Product Name', O.UnitPrice AS Price, " +
"CAST(O.Quantity AS varchar) AS Quantity, O.UnitPrice * O. Quantity AS Value " +
"FROM Orders AS ord, [Order Details] AS O, Products AS P " +
"WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID";
System.Data.SqlClient.SqlDataAdapter adp =
new System.Data.SqlClient.SqlDataAdapter(sQueryString, sqlConnection);
DataSet ds = new DataSet();
adp.Fill(ds);
ExcelAutoFormat xlsAutoFormat = new ExcelAutoFormat();
ExcelStyle xlsHeaderStyle = new ExcelStyle(Color.get_LightGreen());
xlsHeaderStyle.setFontSize(12);
xlsAutoFormat.setHeaderRowStyle(xlsHeaderStyle);
ExcelStyle xlsEvenRowStripesStyle = new ExcelStyle(Color.get_FloralWhite());
xlsEvenRowStripesStyle.setFormat("$0.00");
xlsEvenRowStripesStyle.setHorizontalAlignment(Alignment.ALIGNMENT_LEFT);
xlsAutoFormat.setEvenRowStripesStyle(xlsEvenRowStripesStyle);
ExcelStyle xlsOddRowStripesStyle = new ExcelStyle(Color.FromArgb(240, 247, 239));
xlsOddRowStripesStyle.setFormat("$0.00");
xlsOddRowStripesStyle.setHorizontalAlignment (Alignment.ALIGNMENT_LEFT);
xlsAutoFormat.setOddRowStripesStyle(xlsOddRowStripesStyle);
ExcelStyle xlsLeftColumnStyle = new ExcelStyle(Color.get_FloralWhite());
xlsLeftColumnStyle.setFormat("mm/dd/yyyy");
xlsLeftColumnStyle.setHorizontalAlignment(Alignment.ALIGNMENT_LEFT);
xlsAutoFormat.setLeftColumnStyle(xlsLeftColumnStyle);
Console.WriteLine("Writing file C:\\Samples\\Tutorial02 - export DataSet to Excel with formatting.xlsx.");
workbook.easy_WriteXLSXFile_FromDataSet(
"c:\\Samples\\Tutorial02 - export DataSet to Excel with formatting.xlsx", ds, xlsAutoFormat, "Sheet1");
String sError = workbook.easy_getError();
if (sError.Equals(""))
Console.Write("\nFile successfully created. Press Enter to Exit...");
else
Console.Write("\nError encountered: " + sError + "\nPress Enter to Exit...");
sqlConnection.Close();
workbook.Dispose();
ds.Dispose();
sqlConnection.Dispose();
adp.Dispose();
Console.ReadLine();
}
}
|