Skip to content

Commit da19500

Browse files
author
shiDian
committed
first_commit
0 parents  commit da19500

File tree

36 files changed

+889
-0
lines changed

36 files changed

+889
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

ExcelLib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

ExcelLib/build.gradle

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.1"
6+
7+
defaultConfig {
8+
minSdkVersion 19
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
15+
}
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
}
23+
24+
dependencies {
25+
compile fileTree(include: ['*.jar'], dir: 'libs')
26+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27+
exclude group: 'com.android.support', module: 'support-annotations'
28+
})
29+
compile 'com.android.support:appcompat-v7:25.1.0'
30+
testCompile 'junit:junit:4.12'
31+
compile files('libs/jxl.jar')
32+
}

ExcelLib/libs/jxl.jar

688 KB
Binary file not shown.

ExcelLib/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in C:\Users\Administrator\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.shidian.excel;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.shidian.excel.test", appContext.getPackageName());
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.shidian.excel">
2+
3+
<application android:allowBackup="true" android:label="@string/app_name"
4+
android:supportsRtl="true">
5+
6+
</application>
7+
8+
</manifest>
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package com.shidian.excel;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
import android.widget.Toast;
6+
7+
8+
import java.io.File;
9+
import java.io.FileInputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
import jxl.Workbook;
16+
import jxl.WorkbookSettings;
17+
import jxl.format.Colour;
18+
import jxl.write.Label;
19+
import jxl.write.WritableCell;
20+
import jxl.write.WritableCellFormat;
21+
import jxl.write.WritableFont;
22+
import jxl.write.WritableSheet;
23+
import jxl.write.WritableWorkbook;
24+
import jxl.write.WriteException;
25+
26+
public class ExcelUtils {
27+
public static WritableFont arial14font = null;
28+
29+
public static WritableCellFormat arial14format = null;
30+
public static WritableFont arial10font = null;
31+
public static WritableCellFormat arial10format = null;
32+
public static WritableFont arial12font = null;
33+
public static WritableCellFormat arial12format = null;
34+
35+
public final static String UTF8_ENCODING = "UTF-8";
36+
public final static String GBK_ENCODING = "GBK";
37+
38+
39+
/**
40+
* 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
41+
*/
42+
public static void format() {
43+
try {
44+
arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
45+
arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);
46+
arial14format = new WritableCellFormat(arial14font);
47+
arial14format.setAlignment(jxl.format.Alignment.CENTRE);
48+
arial14format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
49+
arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);
50+
51+
arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
52+
arial10format = new WritableCellFormat(arial10font);
53+
arial10format.setAlignment(jxl.format.Alignment.CENTRE);
54+
arial10format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
55+
arial10format.setBackground(Colour.GRAY_25);
56+
57+
arial12font = new WritableFont(WritableFont.ARIAL, 10);
58+
arial12format = new WritableCellFormat(arial12font);
59+
arial10format.setAlignment(jxl.format.Alignment.CENTRE);//对齐格式
60+
arial12format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); //设置边框
61+
62+
} catch (WriteException e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
67+
/**
68+
* 初始化Excel
69+
* @param fileName
70+
* @param colName
71+
*/
72+
public static void initExcel(String fileName, String[] colName) {
73+
format();
74+
WritableWorkbook workbook = null;
75+
try {
76+
File file = new File(fileName);
77+
if (!file.exists()) {
78+
file.createNewFile();
79+
}
80+
workbook = Workbook.createWorkbook(file);
81+
WritableSheet sheet = workbook.createSheet("成绩表", 0);
82+
//创建标题栏
83+
sheet.addCell((WritableCell) new Label(0, 0, fileName,arial14format));
84+
for (int col = 0; col < colName.length; col++) {
85+
sheet.addCell(new Label(col, 0, colName[col], arial10format));
86+
}
87+
sheet.setRowView(0,340); //设置行高
88+
89+
workbook.write();
90+
} catch (Exception e) {
91+
e.printStackTrace();
92+
} finally {
93+
if (workbook != null) {
94+
try {
95+
workbook.close();
96+
} catch (Exception e) {
97+
e.printStackTrace();
98+
}
99+
}
100+
}
101+
}
102+
103+
@SuppressWarnings("unchecked")
104+
public static <T> void writeObjListToExcel(List<T> objList,String fileName, Context c) {
105+
if (objList != null && objList.size() > 0) {
106+
WritableWorkbook writebook = null;
107+
InputStream in = null;
108+
try {
109+
WorkbookSettings setEncode = new WorkbookSettings();
110+
setEncode.setEncoding(UTF8_ENCODING);
111+
in = new FileInputStream(new File(fileName));
112+
Workbook workbook = Workbook.getWorkbook(in);
113+
writebook = Workbook.createWorkbook(new File(fileName),workbook);
114+
WritableSheet sheet = writebook.getSheet(0);
115+
116+
// sheet.mergeCells(0,1,0,objList.size()); //合并单元格
117+
// sheet.mergeCells()
118+
119+
for (int j = 0; j < objList.size(); j++) {
120+
ArrayList<String> list = (ArrayList<String>) objList.get(j);
121+
for (int i = 0; i < list.size(); i++) {
122+
sheet.addCell(new Label(i, j + 1, list.get(i),arial12format));
123+
if (list.get(i).length() <= 5){
124+
sheet.setColumnView(i,list.get(i).length()+8); //设置列宽
125+
}else {
126+
sheet.setColumnView(i,list.get(i).length()+5); //设置列宽
127+
}
128+
}
129+
sheet.setRowView(j+1,350); //设置行高
130+
}
131+
132+
writebook.write();
133+
Toast.makeText(c, "导出到手机存储中文件夹Record成功", Toast.LENGTH_SHORT).show();
134+
} catch (Exception e) {
135+
e.printStackTrace();
136+
} finally {
137+
if (writebook != null) {
138+
try {
139+
writebook.close();
140+
} catch (Exception e) {
141+
e.printStackTrace();
142+
}
143+
144+
}
145+
if (in != null) {
146+
try {
147+
in.close();
148+
} catch (IOException e) {
149+
e.printStackTrace();
150+
}
151+
}
152+
}
153+
154+
}
155+
}
156+
157+
//----------------------------------读------------------------------------
158+
159+
// public static List<BillObject> read2DB(File f, Context con) {
160+
// ArrayList<BillObject> billList = new ArrayList<BillObject>();
161+
// try {
162+
// Workbook course = null;
163+
// course = Workbook.getWorkbook(f);
164+
// Sheet sheet = course.getSheet(0);
165+
//
166+
// Cell cell = null;
167+
// for (int i = 1; i < sheet.getRows(); i++) {
168+
// BillObject tc = new BillObject();
169+
// cell = sheet.getCell(1, i);
170+
// tc.setFood(cell.getContents());
171+
// cell = sheet.getCell(2, i);
172+
// tc.setClothes(cell.getContents());
173+
// cell = sheet.getCell(3, i);
174+
// tc.setHouse(cell.getContents());
175+
// cell = sheet.getCell(4, i);
176+
// tc.setVehicle(cell.getContents());
177+
// Log.d("gaolei", "Row"+i+"---------"+tc.getFood() + tc.getClothes()
178+
// + tc.getHouse() + tc.getVehicle());
179+
// billList.add(tc);
180+
//
181+
// }
182+
//
183+
// } catch (Exception e) {
184+
// // TODO Auto-generated catch block
185+
// e.printStackTrace();
186+
// }
187+
// return billList;
188+
// }
189+
//
190+
// public static Object getValueByRef(Class cls, String fieldName) {
191+
// Object value = null;
192+
// fieldName = fieldName.replaceFirst(fieldName.substring(0, 1), fieldName
193+
// .substring(0, 1).toUpperCase());
194+
// String getMethodName = "get" + fieldName;
195+
// try {
196+
// Method method = cls.getMethod(getMethodName);
197+
// value = method.invoke(cls);
198+
// } catch (Exception e) {
199+
// e.printStackTrace();
200+
// }
201+
// return value;
202+
// }
203+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Excel</string>
3+
</resources>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.shidian.excel;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11+
*/
12+
public class ExampleUnitTest {
13+
@Test
14+
public void addition_isCorrect() throws Exception {
15+
assertEquals(4, 2 + 2);
16+
}
17+
}

0 commit comments

Comments
 (0)